我試圖編寫一個方便的方法,返回一個UIButton
並將標題,CGRect和一個操作作爲參數。我的代碼編譯並運行,但當我實際單擊按鈕時崩潰。問題傳遞方法作爲參數
這裏是我的簡便方法:
+ (UIButton *)makeButton:(NSString *)title withRect:(CGRect)rect
withAction:(SEL)selectorX {
// setup button properties
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = rect;
[btn setFont:[UIFont boldSystemFontOfSize:20]];
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
btn.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[btn setBackgroundImage:[[UIImage imageNamed:@"btn_21.png"]
stretchableImageWithLeftCapWidth:10.0
topCapHeight:0.0] forState:UIControlStateNormal];
[btn setTitle:@"TEST" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(selectorX:)
forControlEvents:UIControlEventTouchUpInside];
return btn;
}
這裏是我怎樣,我稱之爲:
- (void)viewDidLoad {
[super viewDidLoad];
btn = [Utilities makeButton:@"TEST" withRect:CGRectMake(10, 100, 236, 45)
withAction:@selector(buttonActionTEST:)];
[self.view addSubview:btn];
}
- (void) buttonActionTEST:(id)sender {
[Utilities alert:@"yo yo yeoman"];
}