Adding/removing menu entries
Adding/removing menu entries
- Subject: Adding/removing menu entries
- From: Andreas Schweizer <email@hidden>
- Date: Wed, 11 Jul 2001 11:47:07 +0200
Hi,
I have a main window with a TabView and want to add/remove menu entries
in my MainMenu when the user changes the current TabViewItem. To that
purpose, I've created one NSMenu with the necessary NSMenuItems for each
TabViewItem in the Nib-File.
My code first adds the new menu entries to the main menu and removes the
old entries afterwards. Adding works fine, but [mainMenu
removeItem:curEntry] throws an exception:
Jul 11 11:35:27 cobass[3494] *** Assertion failure in -[NSMenu
removeItem:], NSMenu.m:458
Jul 11 11:35:27 cobass[3494] Item to be removed is not in the menu in
the first place
I'm sure the 'item to be removed' is in the menu, as I can see it in the
menu bar!
I've included the code that does all the work at the end of this mail.
temporaryMenuEntries is an NSMutableDictionary instance variable.
Can anybody help me with this one?
Thanks,
Andy
- (void)_updateMainMenuForTabViewItem:(NSTabViewItem *)tabViewItem
{
id identifier;
NSEnumerator *entryEnumerator;
NSMenu *tempMenu;
NSMenuItem *curEntry;
NSArray *entriesToAdd, *entriesToRemove;
// Get tabViewItem identifier, make sure it is a string
identifier = [tabViewItem identifier];
if (! [identifier respondsToSelector:@selector(isEqualToString:)]) {
NSLog(@"_updateMainMenuForTabViewItem failed: identifier does
not respond to -isEqualToString:");
return;
}
// Which menu items belong to our tabViewItem?
if ([identifier isEqualToString:@"Components"]) tempMenu =
instancesMenu;
else if ([identifier isEqualToString:@"Signals"]) tempMenu =
signalsMenu;
else if ([identifier isEqualToString:@"Simulation"]) tempMenu =
simulationMenu;
else {
NSLog(@"_updateMainMenuForTabViewItem failed: unexpected
identifier");
return;
}
// Nothing to do if tempMenu has already been installed into the
main menu
if (ownerOfTemporaryMenuEntries == tempMenu) return;
entriesToAdd = [tempMenu itemArray];
entriesToRemove = [temporaryMenuEntries copyWithZone:NULL];
// Put the new menu entries into the application main menu
entryEnumerator = [entriesToAdd objectEnumerator];
while ( (curEntry = [entryEnumerator nextObject]) ) {
[tempMenu removeItem:curEntry];
[mainMenu addItem:curEntry];
[temporaryMenuEntries addObject:curEntry];
}
// Remove all currently installed temporary menu entries
entryEnumerator = [entriesToRemove objectEnumerator];
while ( (curEntry = [entryEnumerator nextObject]) ) {
[temporaryMenuEntries removeObject:curEntry];
[mainMenu removeItem:curEntry];
[ownerOfTemporaryMenuEntries addItem:curEntry];
}
ownerOfTemporaryMenuEntries = tempMenu;
[entriesToRemove release];
}