2013-07-29 38 views
-1

到SEL進行選擇我創建了下面的代碼UIButton鑄造下的Objective-C

UIButton* queenButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
queenButton.frame = CGRectMake(0,50,30,30); 
[queenButton setTitle:@"Q" forState:(UIControlState)UIControlStateNormal]; 
[queenButton addTarget:self action:(SEL)[self performSelector:@selector(promotePawnAt:to:) withObject:end withObject:@"Q"] forControlEvents:UIControlEventTouchUpInside]; 

當我這樣做,編譯器給出了Cast of an Objective-C pointer to 'SEL' is disallowed with ARC說,一個錯誤。當我刪除(SEL)演員時,我得到Implicit conversion of an Objective-C pointer to 'SEL' is disallowed with ARC

當按鈕被按下時,我想要做的是讓程序調用promotePawnAt:to:,但該函數需要參數,所以@selector(promotePawnAt:to:)不起作用。

+0

,那麼你所做的假設,而不是邏輯思維......精彩...... – 2013-07-29 20:10:26

+0

(提示:你不能直接你想幹什麼無論是重新設計你的代碼,或者使用一個自定義的按鈕子類和一個屬性來存放任何你想要關聯的信息,或者使用Obejctive-C關聯的對象API等等......) – 2013-07-29 20:11:47

+0

你爲什麼不直接調用這個方法從Button的action方法中? – rdelmar

回答

2

你不能這樣做。您必須創建一個帶有一個參數的方法並將其添加爲按鈕的操作。

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

和內部通話YOUT方法:

-(void)action:(id)button { 
[self promotePawnAt:end to:@"Q"]; 
} 
+0

令人失望的是,我無法這樣做。但是,只有四個可能的參數(Q,R,B和N),所以我想我可以爲這四個參數中的每一個創建一個方法。謝謝您的幫助。 – user1672637

+1

你也可以創建一個字典來將按鈕映射到它們的參數 –