Programatically adding/removing menu items
Programatically adding/removing menu items
- Subject: Programatically adding/removing menu items
- From: Joe Morris <email@hidden>
- Date: Tue, 2 Jul 2002 06:27:57 -0400
Hey everyone,
I'd like to have a menu item that, when selected, changes to another
menu item, and then changes back when selected again. So I'm using it
as a toggle. However, using the code below, when I select it my program
successfully finds it, removes it, and adds the new item, but the item
is disabled. My first guess is that the selector is invalid, but I
don't see how to fix it. Can anyone find the bug in this code, or tell
me what I'm missing?:
- (IBAction)hideIncapacitated:(id)sender {
NSMenu* mainMenu = [NSApp mainMenu];
NSMenuItem* menu = [mainMenu itemWithTitle:@"Battle"];
NSMenu* submenu = [menu submenu];
NSMenuItem* item;
if (hideIncapacitated) {
item = [submenu itemWithTitle:@"Show Incapacitated"];
[submenu removeItem:item];
[submenu insertItemWithTitle:@"Hide Incapacitated"
action:@selector(hideIncapacitated) keyEquivalent:@"p" atIndex:3];
hideIncapacitated = FALSE;
}
else {
item = [submenu itemWithTitle:@"Hide Incapacitated"];
[submenu removeItem:item];
[submenu insertItemWithTitle:@"Show Incapacitated"
action:@selector(hideIncapacitated) keyEquivalent:@"p" atIndex:3];
hideIncapacitated = TRUE;
}
}
As an additional question, is this a good way to have a toggle for this
action? I really want to have a keyboard shortcut for it, so it needs
to be a menu item, but at least one other option would be to have both
Show and Hide always in the menu, and use the setState functions to put
a check next to the one that is in effect. Does anyone think one way is
better than the other for whatever reason?
Thanks!
-Joe
"Anyone who lives within their means
suffers from a lack of imagination."
-Oscar Wilde
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.