问题
I was wondering: what's the difference between writing a selector name with no colon @selector(mySelector)
, or @selector(mySelector:)
with the colon?
As in:
UIBarButtonItem *addButton = [[UIBarButtonItem alloc]initWith...
target:self
action:@selector(addAction:)];
I can't find another example without the colon, but I'm quite sure I have already seen some of them.
回答1:
The colon is needed after the method's name if and only if the method takes an argument.
No function parameters:
-(void)addAction {}
// Use ...@selector(addAction)...
Has parameter:
-(void)addAction:(id)info {}
// Use ...@selector(addAction:)...
回答2:
In certain cases, the number of colons can determine arguments. For example, if you pass in an action method with one colon, it'll send the sender
as the first argument. If you pass in a selector with two colons, you'll get the event
as well. No colon means, obviously, no arguments.
来源:https://stackoverflow.com/questions/7310392/use-colon-or-not-with-selectors