Re: Problem with NSMenu -addItem and -removeItem
Re: Problem with NSMenu -addItem and -removeItem
- Subject: Re: Problem with NSMenu -addItem and -removeItem
- From: Andreas Schweizer <email@hidden>
- Date: Fri, 7 Sep 2001 20:14:23 +0200
Am Freitag, 7. September 2001 um 19:37 schrieb Andrew Platzer:
On Tuesday, September 4, 2001, at 02:15 , Andreas Schweizer wrote:
I'm having a problem with the NSMenu / NSMenuItem classes. I want a
certain menu to be visible in the menu bar only if a certain window is
visible. To that purpose, I've implemented the two delegate messages
of NSWindow:
- (void)windowDidBecomeMain:(NSNotification *)aNotification
{
[mainMenu addItem:simulationMenu];
}
- (void)windowDidResignMain:(NSNotification *)aNotification
{
[mainMenu removeItem:simulationMenu];
}
The menu is added to the menu bar when the window becomes main for the
first time, and is properly removed from the menu bar when the window
resigns main.
Unfortunately, this works only once. After the menu has been removed
once,
it does never appear in the menu bar again :-(
(I've added code to print the [mainMenu description], and the
interesting thing is that it looks all right -- the simulationMenu is
correctly added and removed; it simply doesn't appear on the screen!)
Can anybody help me with that? Is there something like the old
DrawMenuBar that you have to call to update the menu bar?
This was a bug in 10.0.x and has been fixed for 10.1. Unfortunately,
the only workaround for 10.0.x is to create a new menu instead of
adding the old one back in again.
Andrew
Thanks Andrew for the information. I've written two work-around methods,
and the menu item is now properly added and removed. I got an
interesting exception first when I tried to remove the item from the
copy of the main menu ('Item to be removed is not in the menu in the
first place' - huh???). It works only when I remove the item from the
main menu before I make the copy.
- (void)_removeItemFromMainMenu:(NSMenuItem *)item
{
NSMenu *mainMenu = [NSApp mainMenu];
NSMenu *newMenu;
if (mainMenu) {
[mainMenu removeItem:item];
newMenu = [mainMenu copy];
[NSApp setMainMenu:newMenu];
}
}
Thanks,
Andy