我试图编写一个方便的方法,返回一个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"];
}