为 iOS 扩展 UIButton 时出现问题

发布于 2024-11-09 11:15:41 字数 1611 浏览 5 评论 0原文

我尝试扩展 UIButton,但在 ColorButton 实现文件的初始化程序中不断收到“EXC_BAD_ACCESS”。

ColorButton 的标题。

#import <Foundation/Foundation.h>


@interface ColorButton : UIButton {
    UIImage * originalImage;
}

@property (nonatomic,readonly) NSString * buttonName;

-(id) initButtonWithName:(NSString *) color;
-(void) setOriginalImage;
-(void) setImage:(UIImage *) image;
@end

颜色按钮实现

#import "ColorButton.h"

@implementation ColorButton

@synthesize buttonName;

-(id) initButtonWithName:(NSString *) color {
    if ((self = (ColorButton *)[UIButton buttonWithType:UIButtonTypeCustom])) {
        buttonName = color;
        [self setTitle:buttonName forState:UIControlStateNormal]; //This is the line of the "EXC_BAD_ACCESS" error.
        [self setBackgroundImage:[self backgroundImageForDevice:color] forState:UIControlStateNormal]; // This line gets the error too. If I comment the line before it out.
    }
    return self;
}

-(UIImage *) backgroundImageForDevice:(NSString *) color {
        color = [color stringByAppendingString:@"Bubble"];
    if ([[[UIDevice currentDevice] model] isEqualToString:@"iPad"] ||[[[UIDevice currentDevice] model] isEqualToString:@"iPad Simulator"]) {
        color = [color stringByAppendingString:@"-iPad"];
    }
    color = [color stringByAppendingString:@".png"];
    return [UIImage imageNamed:color];
}

-(void) setOriginalImage {
    [self setBackgroundImage:originalImage forState:UIControlStateNormal];
}

-(void) setImage:(UIImage *) image {
    [self setImage:image forState:UIControlStateNormal];
}
@end

I trying to extend UIButton but I keep getting a "EXC_BAD_ACCESS", inside the initializer of ColorButton's implementation file.

Header of ColorButton.

#import <Foundation/Foundation.h>


@interface ColorButton : UIButton {
    UIImage * originalImage;
}

@property (nonatomic,readonly) NSString * buttonName;

-(id) initButtonWithName:(NSString *) color;
-(void) setOriginalImage;
-(void) setImage:(UIImage *) image;
@end

ColorButton Implementation

#import "ColorButton.h"

@implementation ColorButton

@synthesize buttonName;

-(id) initButtonWithName:(NSString *) color {
    if ((self = (ColorButton *)[UIButton buttonWithType:UIButtonTypeCustom])) {
        buttonName = color;
        [self setTitle:buttonName forState:UIControlStateNormal]; //This is the line of the "EXC_BAD_ACCESS" error.
        [self setBackgroundImage:[self backgroundImageForDevice:color] forState:UIControlStateNormal]; // This line gets the error too. If I comment the line before it out.
    }
    return self;
}

-(UIImage *) backgroundImageForDevice:(NSString *) color {
        color = [color stringByAppendingString:@"Bubble"];
    if ([[[UIDevice currentDevice] model] isEqualToString:@"iPad"] ||[[[UIDevice currentDevice] model] isEqualToString:@"iPad Simulator"]) {
        color = [color stringByAppendingString:@"-iPad"];
    }
    color = [color stringByAppendingString:@".png"];
    return [UIImage imageNamed:color];
}

-(void) setOriginalImage {
    [self setBackgroundImage:originalImage forState:UIControlStateNormal];
}

-(void) setImage:(UIImage *) image {
    [self setImage:image forState:UIControlStateNormal];
}
@end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑看君怀她人 2024-11-16 11:15:41

您无法将 UIButton * 实例强制转换为 ColorButton * 类型。

您必须记住,ColorButton 继承自 UIButton,而不是相反,这意味着 ColorButton 的每个实例都是一个 UIButton 根据定义,但反之则不然。

这是另一个具有完全相同相同问题的线程:)

目标 C:从 UIButton 类的子类创建的按钮不起作用

You can not cast a UIButton * instance to a ColorButton * type.

You have to remember that you ColorButton inherits from UIButton and not the other way, this means that every instance of ColorButton is a UIButton by definition, but the opposite is not true.

Here is another thread that has the exact same issue :)

objective C: Buttons created from subclass of UIButton class not working

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文