2011-06-19 40 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);