开发者

iPhone: Difference in @selector syntax

开发者 https://www.devze.com 2023-03-14 11:15 出处:网络
Most likely a dumb question, but what\'s the difference between: UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:@\"Join\" style:UIBarButtonItemStylePlain

Most likely a dumb question, but what's the difference between:

UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:@"Join" style:UIBarButtonItemStylePlain
                                target:self action:@selector开发者_StackOverflow中文版(pressJoinButton)];

and

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

Notice how one is pressJoinButton, and the other is pressJoinButton:


The main (and only) difference is that pressJoinButton and pressJoinButton: are to totally distinct and unrelated selectors. This is mainly because the colons are part of a method's name in ObjectiveC.

The difference between pressJoinButton and pressJoinButton: is about the same as the difference between void pressJoinButton(); and void pressJoinButton(id sender); when declared in a language with support for function overloading. They are two totally different methods/functions.

pressJoinButton would refer to a method of a pattern like this:

- (void)pressJoinButton;

while pressJoinButton: would refer to a method of a pattern like this:

- (IBAction)pressJoinButton:(id)sender;

This also applies for methods with multiple arguments:

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

which translates to the following selector:

doFoo:withBar:inFoobar:

and which in function-like syntaxt you would probably have declared like this:

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


The colon is used to add arguments to the method you are calling, so if pressJoinButton had zero arguments, it would be:

pressJoinButton

If it had one argument, it would be:

pressJoinButton:

if it had 2 arguments, it would be:

pressJoinButton:withArg1:

if it had 3 arguments it would be:

pressJoinButton:withArg1:withArg2:

etc

Hope this helps!


For first sample action declaration is:

- (void)pressJoinButton;

For second:

- (void)pressJoinButton:(id)sender;

0

精彩评论

暂无评论...
验证码 换一张
取 消