2012-04-17 23 views
0

我想要使用addTarget:操作:forControlEvents但我收到一個無法識別的選擇器的運行時異常。使用addTarget獲取運行時異常:操作:forControlEvents

這被稱爲UIView子類initWithFrame。 下面是我的代碼:

myButton = [[UIButton alloc] initWithFrame:frame]; 
[myButton addTarget:self action:&selector(displayList:) 
         forControlEvents:UIControlEventTouchUpInside]; 

我的方法:

-(void)displayList:(id)sender 

當我點擊它收到以下消息按鈕:

-[MyClass displayList:]: unrecognized selector sent to instance. Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: -[MyClass displayList:]: unrecognized selector sent to instance.

MyClass的是一個自定義控制是一個UIView子類與UIButton and UILabel。這個類將被放置在另一個應用程序的ViewController上。

我不知道我缺少什麼。任何幫助將不勝感激。

回答

2

應改爲

[myButton addTarget:self action:@selector(displayList:) forControlEvents:UIControlEventTouchUpInside]; 

@代替&

0
[myButton addTarget:self action:&selector(displayList:) forControlEvents:UIControlEventTouchUpInside]; 

的語法是@selector(顯示列表:)
此外,還有您的按鈕可能不會觸發事件的變化。嘗試使用這種方式創建它:

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; //this will return an autoreleased UIButton object, be careful. 
button.frame = CGRectMake(...); 
... 
+0

它在我的代碼中。這只是一個錯字。 – Greg 2012-04-17 14:54:13

+0

這可能是一個Visiblity問題?如果displayList在自定義控件類中,並且該類正在另一個應用程序中使用,它是否會找到displayList方法? – Greg 2012-04-17 18:38:27

+0

如果您已將自己添加爲目標,則必須在您擁有的自定義UIView類中實現displayList方法。 – graver 2012-04-17 18:49:22