UIButton addTarget:self ...导致“无法识别选择器”错误

时间:2011-05-27 08:19:28

标签: objective-c uibutton

我初始化了我的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];

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

选择器按钮点击:如下所示:

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

我做错了什么?

2 个答案:

答案 0 :(得分:1)

使用以下内容添加操作:

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

然后使用

添加按钮
[view addSubview:it];

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

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

答案 1 :(得分:0)

您可以使用+(id)buttonWithType:(UIButtonType)buttonType

创建按钮