2011-05-27 36 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