0

我有一种制作按钮的方法。参数是文本颜色位置 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 { return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); }

4

1 回答 1

1

我找到了..我在我的方法中添加了目标选择器,我认为选择器和方法必须在同一个地方,所以我将 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];

它有效!

于 2013-09-13T13:46:39.367 回答