我有一个制作按钮的方法。参数是文本颜色位置x位置y按钮类型和方法(注意:此方法在类中)
-(UIButton *)makeButton :(NSString *)string :(UIColor*)textColor :(CGFloat)posx :(CGFloat)posy :(UIButtonType)type :(SEL)run
{
UIButton *button =[UIButton buttonWithType:type];
[button addTarget:self action:run forControlEvents:UIControlEventTouchUpInside];
[button setTitle:string forState:UIControlStateNormal];
button.titleLabel.font =[UIFont systemFontOfSize:11];
[button setTitleColor:textColor forState:UIControlStateNormal];
CGSize stringSize =[string sizeWithFont:[UIFont systemFontOfSize:11]];
[button setFrame:CGRectMake(posx, posy, stringSize.width+10, stringSize.height+5)];
return button;
}
我正在调用这样的方法。
UIButton *btnGidenler= [methods makeButton:anlik :[methods RGBR:0 G:135 B:222 A:1.0] :10 :5:(UIButtonType)UIButtonTypeCustom :@selector(gidenKisiler)];
[viewGidenler addSubview:btnGidenler];
和@selector(btnGidenler)
基本方法-(void)gidenKisiler{ //just alert }
当我点按按钮时,我的应用程序崩溃了。为什么选择器不工作?
粉碎错误
@autoreleasepool { 返回UIApplicationMain(argc,argv,nil,NSStringFromClass([AppDelegate class])); }
答案 0 :(得分:1)
我找到了..我在我的方法中添加了目标选择器,我认为选择器和方法必须在同一个地方,所以我在返回按钮Imean下移动addTarget部分就像对不起我的英语
UIButton *btnGidenler= [methods makeButton:anlik :[methods RGBR:0 G:135 B:222 A:1.0] :10 :5:(UIButtonType)UIButtonTypeCustom :@selector(gidenKisiler)];
[button addTarget:self action:run forControlEvents:UIControlEventTouchUpInside];
[viewGidenler addSubview:btnGidenler];
它有效!