9

I am creating a menu programmatically:

+ (void)refreshStatusMenu {
    for (NSDictionary *dict in kbMsgSet) {
        NSString *msj = [dict objectForKey:@"msj"];
        NSString *mid = [dict objectForKey:@"mid"]; // <- this would be http://www.blah.com 

        msg_item = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@"%@", msj] action:@selector(goToURL:mid:) keyEquivalent:@""];

        [msg_item setTarget:[self class]];
        [sm insertItem:msg_item atIndex:(i_msg)];
        i_msg++;
        //...
    }
}

How do I pass a parameter to @selector(goToURL:), so that on item-click I could call:

+ (void)goToURL:(id)obj {
    NSLog(@"Open url:...%@", obj);
}

If I try passing @selector(goToURL:var2:) I get uncaught exception error.

janeh
  • 3,734
  • 7
  • 26
  • 43
  • Could you provide the actual code you are calling that is causing the crash? – Carl Veazey Oct 02 '12 at 01:39
  • added full function where I'm calling `action:@selector(goToURL:mid:)`... I also get 'Unused variable mid' warning in XCode... I am using it! – janeh Oct 02 '12 at 01:49

1 Answers1

8

You can't have 2 parameters to an action method. There should only be one, the sender, which in this case would be the menu item. See the answer to this question for a way to attach extra information to a menu item and retrieve it in the action method.

Community
  • 1
  • 1
JWWalker
  • 22,385
  • 6
  • 55
  • 76