我有一个 AuthButtons.h 标头:
#import <UIKit/UIKit.h>
@interface AuthButtons : UIButton
@property (nonatomic, weak) UIColor *backgroundColor;
@end
AuthButtons.m 类的实现如下:
#import "AuthButtons.h"
@implementation AuthButtons
- (void)setBackgroundColor:(UIColor *)backgroundColor
{
backgroundColor = [UIColor blueColor];
}
- (UIColor *)backgroundColor
{
return [UIColor whiteColor];
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
然后在我的 viewcontroller.m 类中,我有:
@interface ViewController ()
@property (nonatomic, weak) AuthButtons *registerButton;
@end
...
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
_registerButton = [AuthButtons buttonWithType:UIButtonTypeSystem];
[_registerButton setTitle:@"Register" forState:UIControlStateNormal];
[_registerButton setBackgroundColor:[UIColor whiteColor]];
_registerButton.frame = CGRectMake(0.0, 0.0, self.screenWidth / 2.0, 100.0);
[_registerButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
}
当我尝试创建按钮时,它一直在崩溃。我错过了什么?