Cocoa - How to pass parameter using NSMenuItem action?

I create 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 to pass a parameter @selector(goToURL:), so when I click on an element I could call:

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

If I try to pass @selector(goToURL:var2:), I get an uncaught exception error.

+5
source share
1 answer

You cannot have 2 parameters for an action method. There should be only one sender, which in this case will be a menu item. See the Answer to this question for a way to add additional information to a menu item and get it in the action method.

+6
source

All Articles