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

标签 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 对象。该自定义方法用于任何 View Controller 。该自定义方法需要传入参数,如 x、y、宽度、高度、按钮标题和作为事件处理程序并传入的选择器,如“(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;
}

然后在 View Controller .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

同一 View Controller .m 文件中按钮的事件处理程序的定义如下:

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

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

附言如果我将自定义方法重写为签名中没有“(SEL)selector”参数的替代方法,并让调用该自定义方法的 Controller View 完成编码选择器的工作,那么没有 异常:

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

最佳答案

问题出在辅助类中:

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

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

您应该在辅助方法中添加 inController 参数,以告知选择器方法放置在哪个 Controller 中。

在您的特定情况下,辅助方法应如下所示(请注意,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;
}

你应该这样从你的 Controller 调用它:

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

关于ios - 将选择器传递给自定义方法以编程方式创建 UIButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19891922/

相关文章:

ios - UIView 类别和投影——应用两次

ios - UIAlertView iOS 8 beta 5 中无标题的 UI 问题

ios - 在没有打开版本控制的情况下更改核心数据模型

ios - 配置与 iOS 6 兼容的 iOS 7 应用程序

iphone - 如何在单击 iPhone 中的 UIButton 时打开相机?

ios - 使选定的按钮标题标签为大写

ios - 如何向按钮添加一些功能?

ios - reloadRowsAtIndexPaths 的奇怪行为

ios - 为什么使用 Sharekit 将图像发布到 Facebook 如此缓慢?

objective-c - Tapku 日历不显示