【问题标题】:UIButton addTarget: self ... causes "Does not recognize selector" errorUIButton addTarget: self ... 导致“无法识别选择器”错误
【发布时间】:2011-05-27 16:27:37
【问题描述】:

我初始化了我的 UIButton-deriver 就像这样:

Button * it = [[Button alloc] initWithFrame:CGRectMake(x, y, image.size.width, image.size.height)];

然后,我做下一个:

[(UIButton *)self addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:self];

第一行导致“不识别选择器”错误。

Selector buttonClicked: 看起来像这样:

-(IBAction) buttonClicked:(id)sender { 
    if (action) action();
    else NSLog(@"Clicked.\n");
} 

我做错了什么?

【问题讨论】:

    标签: objective-c uibutton


    【解决方案1】:

    添加动作:

    [it       addTarget:self 
                 action:@selector(buttonClicked:) 
       forControlEvents:UIControlEventTouchUpInside];
    

    然后添加按钮

    [view addSubview:it];
    

    并且不要使用 init 创建 UIButtons...使用类方法 + buttonWithType:

    小心继承 UIButton,我不确定你是否真的想要这个。看看create uibutton subclass

    【讨论】:

      【解决方案2】:

      您使用+(id)buttonWithType:(UIButtonType)buttonType 创建按钮

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-26
        • 2018-06-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多