使用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:'
你 initWithCoder 最后 return nil 是手误吧?