Question

I have an NSMenu with 2 default items (at the top of it).

Then the rest of the menu will be populated.

What I want is to "save" the state of the initial menu (with the two items in it), and add items to that (original) menu; and not just keep adding and adding.

So, if I have to add other items, those will have to be added in the initial menu.

Example :

  • Menu At the beginning : A B |
  • Menu After User does This : A B | C D E
  • Menu After User does That : A B | G H I J K
  • etc

How should I go about that? (I tried "saving" the initial menu, and then "copying" that to the current menu, so that I could add items on top of it, but it didn't work... :-S)


[DOC_UI setCurrentMenu:[[DOC_UI originalMenu] copy]];

// I'm adding the items here

for (NSMenuItem* mi in [[DOC_UI currentMenu] itemArray])
      NSLog(@"orig :: mi : %@",[mi title]);

/* The weird thing is that the items ARE NSlogged, but the change is NOT reflected. */
/* (When I was just adding to the existing items, the items did show up...) */
Was it helpful?

Solution

If you're talking about menu items of a menu, you can remove all but the first 2 items in a loop when you want to go back to your original menu. If you have a menu with 2 default items called userMenu, just do this:

-(void)removeMenuItems {
for (NSInteger i=self.userMenu.itemArray.count - 1; i>1; i--) {
    [self.userMenu removeItemAtIndex:i];
}

}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top