2011-06-19 43 views
1

最有可能是愚蠢的問題,但有什麼區別:iPhone:差異@selector語法

UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:@"Join" style:UIBarButtonItemStylePlain 
           target:self action:@selector(pressJoinButton)]; 

UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:@"Join" style:UIBarButtonItemStylePlain 
           target:self action:@selector(pressJoinButton:)]; 

注意如何一個是pressJoinButton,另一個是pressJoinButton:

+0

可能重複[當使用一個冒號@選擇器](http://stackoverflow.com/questions/4953623/when-to-use-a-colon-with-a-selector) –

+0

可能解決這個問題的答案:http://stackoverflow.com/questions/4953623 /時使用的-A-結腸上帶有一個選擇器 – gram

回答

6

冒號用於爲您調用的方法添加參數,因此如果pressJoinButton具有零參數,那將是:

pressJoinButton 

如果它有一個說法,那就是:

pressJoinButton: 

,如果它有2個參數,這將是:

pressJoinButton:withArg1: 

如果它有3個參數,那就是:

pressJoinButton:withArg1:withArg2: 

etc

希望這有助於!

1

對於第一樣本動作聲明爲:

- (void)pressJoinButton;

對於第二:

- (void)pressJoinButton:(id)sender;

8

主要(也是唯一的)不同的是,pressJoinButtonpressJoinButton:是完全不同的和不相關的選擇。這主要是因爲冒號是ObjectiveC中方法名稱的一部分。

pressJoinButtonpressJoinButton:之間的區別是大致相同void pressJoinButton();void pressJoinButton(id sender);之間的差異與函數重載支持的語言聲明時。它們是兩種完全不同的方法/功能。

pressJoinButton將提及的圖案的方法是這樣的:

- (void)pressJoinButton; 

pressJoinButton:將提及的圖案的像這樣的方法:

- (IBAction)pressJoinButton:(id)sender; 

這也適用於有多重參數的方法:

- (void)doFoo:(Foo *)foo withBar:(Bar *)bar inFoobar:(Foobar *)foobar; 

它轉換爲以下選擇:

doFoo:withBar:inFoobar: 

並且在功能上類似syntaxt你或許會聲明如下:

void doFooWithBarInFoobar(Foo *foo, Bar *bar, Foobar *foobar);