Removing a Menu

To remove a menu item from a menu, you send removeItem: or removeItemAtIndex: to the NSMenu object managing the menu item.

To remove an entire menu from the menu bar, you use the same technique. The menus in the menu bar are themselves items of another menu: the root menu, or main menu. To get the main menu, send mainMenu to NSApp, the global application instance. Then send removeItem: to the main menu; or find the index of the menu to be removed and send removeItemAtIndex: to the main menu. Listing 1 illustrates the latter procedure.

Listing 1  Removing a menu from the menu bar

- (IBAction)removeMenu:(id)sender {
    NSMenu* rootMenu = [NSApp mainMenu];
    // sender is an NSMenuItem
    [rootMenu removeItemAtIndex:[rootMenu indexOfItemWithSubmenu:[sender menu]]];