iPhone:@selector语法的差异

时间:2011-06-19 18:47:05

标签: iphone objective-c

很可能是一个愚蠢的问题,但有什么区别:

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:

3 个答案:

答案 0 :(得分: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:

和函数式合成文件中你可能会这样声明:

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

答案 1 :(得分:6)

冒号用于向您调用的方法添加参数,因此如果pressJoinButton的参数为零,则为:

pressJoinButton

如果它有一个参数,那就是:

pressJoinButton:

如果它有2个参数,那就是:

pressJoinButton:withArg1:

如果它有3个参数,那就是:

pressJoinButton:withArg1:withArg2:

希望这有帮助!

答案 2 :(得分:1)

对于第一个样本行动声明是:

- (void)pressJoinButton;

第二名:

- (void)pressJoinButton:(id)sender;