Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSMenuItem not being enabled?

I have an NSMenuItem called history which resides in a NSMenu called menu. When my program starts history doesn't have a submenu so its disabled. Then at some point, I need a submenu for history, so I create it, make it a submenu of history. An arrow appears beside history which tells me that the submenu is there. But history is still disabled. I have tried setEnabled but doesn't work. Please help. Here my code:

This is when I create my menu, and history as you can see an NSMenuItem in menu.

    menu = [[NSMenu alloc] initWithTitle:@"Menu"];
[[menu addItemWithTitle:@"History" action:nil keyEquivalent:@""] setTarget:self];
[[menu addItemWithTitle:@"Settings" action:@selector(loadSettings:) keyEquivalent:@""] setTarget:self];
[[menu addItemWithTitle:@"Quit" action:@selector(terminateApp:) keyEquivalent:@""] setTarget:self];

At this point, history is disabled (greyed out). Then somewhere in the program I need to have a submenu for history so:

        if (historyMenu == nil) {
        historyMenu = [[NSMenu alloc] initWithTitle:@"Lyrics history"];
        [menu setSubmenu:historyMenu forItem:[menu itemWithTitle:@"History"]];
    }

I now see an arrow beside history, but it's still greyed out.

Please help, I have been trying to figure this out for last 2 hours. Thanks.

like image 991
user635064 Avatar asked Feb 26 '11 00:02

user635064


1 Answers

You are setting the target but have a nil action. Try not setting the target. This may leave it enabled all the time in which case you may have to manually enable or disable the menu item.

Here is the documentation on how menu items get enabled.

like image 164
Jon Steinmetz Avatar answered Oct 09 '22 06:10

Jon Steinmetz