2011-06-19 6 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:)]; 

1はpressJoinButtonであり、他方はpressJoinButtonされている様子がわかります

+0

可能重複@セレクタ](http://stackoverflow.com/questions/4953623/when-to-use-a-colon-with-a-selector) –

+0

この質問の回答でおそらく対処しています:http://stackoverflow.com/questions/4953623/when-to-use-a-colon-a-selector – gram

答えて

6

コールするメソッドに引数を追加するためにコロンが使用されるため、pressJoinButtonに引数がない場合は、

それは一つの引数を持っていた場合
pressJoinButton 

、それは次のようになります。それは2つの引数を持っていた場合

pressJoinButton: 

が、それは次のようになります。それは3つの引数を持っていた場合

pressJoinButton:withArg1: 

が、それは次のようになります。

pressJoinButton:withArg1:withArg2: 

これは役に立ちます。最初のサンプル・アクション宣言の

1

である:秒間

- (void)pressJoinButton;

- (void)pressJoinButton:(id)sender;

8

主(そして唯一の)差がpressJoinButtonpressJoinButton:がに全く異なると無関係であるということですセレクタ。これは主に、コロンがObjectiveCのメソッド名の一部であるためです。関数のオーバーロードをサポートする言語で宣言されたとき

pressJoinButtonpressJoinButton:の違いはvoid pressJoinButton();void pressJoinButton(id sender);の違いとほぼ同じです。それらは2つの全く異なる方法/機能です。

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); 
関連する問題