将选择器传递给自定义方法以编程方式创建UIButton

时间:2013-11-10 16:11:56

标签: ios objective-c uibutton ios7

我在ios 7下使用XCode 5来做一个简单的项目。当我按下以编程方式创建的按钮时出现以下错误:

2013-11-10 09:16:02.969 Project2[446:70b] +[KNSunDynamicUIButton buttonSavePressed:]: unrecognized selector sent to class 0x221424

详细说明:
我创建了一个自定义方法,用于以编程方式创建UIButton对象。该自定义方法用于任何视图控制器。该自定义方法需要传入的参数,如x,y,width,height,button title和selector,它们是一个事件处理程序,并像“(SEL)selector ”一样​​传入。

自定义方法(属于帮助程序类):

+(UIButton*)kNSunSetupWithSelector:(SEL)selector    withX:(int)x y:(int)y width:(int)width height:(int)height
                       buttonTitle:(NSString*)buttonTitle backGroundColor:(CGColorRef) backGroundColor
{

    UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    [button setTitle:@"Save" forState:UIControlStateNormal];
    button.frame = CGRectMake(x, y, width, height);
    button.backgroundColor = [UIColor greenColor];

// selector is used over here
    [button addTarget:self
                    action:selector
          forControlEvents:UIControlEventTouchDown];

    return button;
}

然后在视图控制器.m文件中,我将该自定义方法称为:

-(void)setUpButtons
{
            SEL selector = @selector(buttonSavePressed:);
            _buttonSave = [KNSunDynamicUIButton kNSunSetupWithSelector:selector withX:470 y:410 width:160 height:40 buttonTitle:@"Save" backGroundColor:(_buttonSaveColor)];

            [self.view addSubview:_buttonSave];

}

    @interface MyViewController ()
    {
    ...

        // buttons
        UIButton* _buttonSave;
    }
    @end

按钮的事件处理程序在相同的视图控制器.m文件中的定义如下:

- (void)buttonSavePressed:(UIButton*)button
{
    NSLog(@"Button Save clicked.");
}

当我运行我的代码并点击按钮时,我看到如上所述的异常。请帮忙谢谢。

P.S。如果我重写自定义方法作为其签名中没有“(SEL)selector”参数的替代方法,并让调用该自定义方法的控制器视图完成编码选择器的工作,那么 no < / strong>例外:

-(void)setUpButtons
{
    //Note: the codes are in my view controller .m file

_buttonSave = [KNSunDynamicUIButton kNSunSetupWithX:470 y:410 width:160 height:40 buttonTitle:@"Save" backGroundColor:_buttonSaveColor];


   // Note: selector coding is taken care by codes of my view controller instead of by custom method
   [_buttonSave addTarget:self
    action:@selector(buttonSavePressed:)
    forControlEvents:UIControlEventTouchDown];



    [self.view addSubview:_buttonSave];
    [_buttonSave setupView];
}

另一种自定义方法(我不喜欢这种方法,因为它没有处理动态传入选择器):

+(UIButton*)kNSunSetupWithX:(int)x y:(int)y width:(int)width height:(int)height
                   buttonTitle:(NSString*)buttonTitle backGroundColor:(CGColorRef) backGroundColor
{
    UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    [button setTitle:@"Save" forState:UIControlStateNormal];
    button.frame = CGRectMake(x, y, width, height);
    button.backgroundColor = [UIColor greenColor];

    return button;
}

1 个答案:

答案 0 :(得分:4)

问题在于辅助类:

[button addTarget:self action:selector forControlEvents:UIControlEventTouchDown];

在这里你说过帮助类将有你通过选择器的方法,在你的buttonSavePressed:方法中。

你应该在你的帮助方法中添加inController参数,以告知哪个控制器是选择器方法。

在您的特定情况下,辅助方法看起来应该是这样的(注意 inController 参数在选择器参数之后添加):

+(UIButton*)kNSunSetupWithSelector:(SEL)selector
                      inController:(UIViewController*)controller
                             withX:(int)x
                                 y:(int)y
                             width:(int)width
                            height:(int)height
                       buttonTitle:(NSString*)buttonTitle
                   backGroundColor:(CGColorRef) backGroundColor
{

UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

[button setTitle:@"Save" forState:UIControlStateNormal];
button.frame = CGRectMake(x, y, width, height);
button.backgroundColor = [UIColor greenColor];

// selector is used over here
[button addTarget:controller
           action:selector
 forControlEvents:UIControlEventTouchDown];

    return button;
}

你应该通过这种方式从控制器中调用它:

-(void) setupButtons
{
    SEL selector = @selector(buttonSavePressed:);
    _buttonSaveColor = [UIColor orangeColor].CGColor;
    _buttonSave = [KNSunDynamicUIButton kNSunSetupWithSelector:selector
                                                  inController:self
                                                         withX:470
                                                             y:410
                                                         width:160
                                                        height:40
                                                   buttonTitle:@"Save"
                                               backGroundColor:(_buttonSaveColor)];

    [self.view addSubview:_buttonSave];
}