首页 文章

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

提问于
浏览
0

我在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 ”一样传入 .

Custom method (属于助手类):

+(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;
}

然后在 view controller .m file 中,我将该自定义方法称为:

-(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

the same view controller .m file 中按钮的事件处理程序的定义如下:

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

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

附:如果我重写自定义方法作为其签名中没有"(SEL)selector"参数的替代方法,并且让调用该自定义方法的控制器视图完成编码选择器的工作,则会出现 no 异常:

-(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 回答

  • 4

    问题在于辅助类:

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

    在这里你说帮助器类将有方法're passing through selector, in you' re case 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];
    }
    

相关问题