2011-05-27 49 views
1

我初始化我的UIButton,推导器liek这样的:的UIButton addTarget:自...导致 “不承认选择” 错误

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

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

选择buttonClicked:看起来是这样的:

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

我在做什么错?

回答

0

您创建+(id)buttonWithType:(UIButtonType)buttonType

1

按钮与添加的操作:

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

,然后用

[view addSubview:it]; 

添加按钮,并不会产生使用init ...使用UIButtons班级方法+ buttonWithType:

要小心子类UIButton,我不知道你是否真的想要这个。看看create uibutton subclass