我有一個AuthButtons.h標題:建立一個子類UIButton的處理看起來和感覺
#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];
}
當我嘗試創建按鈕時,它不斷崩潰。我錯過了什麼?
仍然在該行崩潰,其他事情必須在 – cdub
@ chris ..什麼錯誤你得到? –
nm,我有一個錯位[外部評論 – cdub