objective C 自定义一个button的类,出现NSGenericException error

使用storyboard拖一个button,然后指定button的custom class,class 如下:

h file:

@interface CustomizationButton : UIButton

@end

m file:

@implementation CustomizationButton

-(id) initWithCoder:(NSCoder *)aDecoder {
if((self = [super initWithCoder:aDecoder])){
    self.layer.cornerRadius = 4.0;
    self.clipsToBounds = true;
    self.backgroundColor = [UIColor colorWithRed:69/255 green:83/255 blue:153/255 alpha:1];
}
return nil;
}

-(void) setHighlighted:(BOOL)highlighted {
[super setHighlighted:highlighted];

if(highlighted){
    self.backgroundColor = [UIColor colorWithRed:83/255 green:98/255 blue:178/255 alpha:1];
} else {
    self.backgroundColor = [UIColor colorWithRed:69/255 green:83/255 blue:153/255 alpha:1];
}
}

@end

错误如下

Terminating app due to uncaught exception 'NSGenericException', reason: 'This coder requires that replaced objects be returned from initWithCoder:'
阅读 3.7k
3 个回答

你 initWithCoder 最后 return nil 是手误吧?

最后应该 return self

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题