• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Adding/removing menu entries
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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];
}


  • Prev by Date: Re: Security framework question
  • Next by Date: is the nextKeyView really used by Cocoa ?
  • Previous by thread: setEditable:FALSE not firing? Or am I doing something I shouldn't?
  • Next by thread: is the nextKeyView really used by Cocoa ?
  • Index(es):
    • Date
    • Thread