开发者

Simple @selector Syntax Error

This seems to be the simplest of problems... yet I can't figure out what's syntactically incorrect with this snippet.

Running this:

UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:appDelega开发者_开发技巧te action:@selector(addImage:NO)];

Returns:

Expected '(' before ')' token

I've looked it over hundreds of times, yet I can't find what's wrong with it. Thanks for any assistance.


You can't have NO in your @selector() directive.

Be careful. UI Actions selectors should have the method signature:

- (void)myMethod:(id)sender;

and not

- (void)myMethod:(BOOL)someBool;

You may need a wrapper method:

- (void)doneAction:(id)sender;
{
    [appDelegate addAction:NO];
}


@selector() can't accept default values, change it to

UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:appDelegate action:@selector(addImage:)];

Edit: To make things clear, when you have - (void)addImage:(BOOL)resize; this is translated to "addImage:" as "method" name (selector) and when you pass over "addImage:NO" you are telling objc to look - (void)addImage:(BOOL)resize NO:(BOOL)nod; // just for the example ending up with wrong (invalid) selector, resulting in double back trace to all super objects calling respondsToSelector(addImage:NO) then throwing BAD_ACCESS.

p.s. Its more then year since I wrote my last line of objc.

Edit 2: You can use wrapper as mentioned above or UIBarButtonItem.tag property

cancelButton.tag = 1; // 1 indicating addImage:NO

- (void) addImage:(id)sender {
    UIBarButtonItem *button = (UIBarButtonItem *)sender;
    if (button.tag == 1) {
        // your value is NO
    } else {
        // your value is YES
    }
}

but I would not go with this anyway. :)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜