Next: 3.6 Creating a standard Up: 3 Menus Previous: 3.4 Fun with NSMenuItem's

3.5 Creating sub-menus

Creating sub-menus is quite easy. For example, to add an Info... sub-menu to your main application menu, you first create a menu representing it:
NSMenu *infoMenu;

infoMenu = AUTORELEASE ([NSMenu new]);
[infoMenu addItemWithTitle: @"Info Panel..." 
          action: @selector (orderFrontStandardInfoPanel:) 
          keyEquivalent: @""];
[infoMenu addItemWithTitle: @"Help..." 
          action: @selector (orderFrontHelpPanel:)
          keyEquivalent: @"?"];
Then, you create an item in the main menu for your info menu, but instead of setting an action and a target for that item, you set the infoMenu as the sub-menu corresponding to that item:
NSMenuItem *menuItem;

menuItem = [menu addItemWithTitle: @"Info..." 
                 action: NULL 
                 keyEquivalent: @""];
[menu setSubmenu: infoMenu  forItem: menuItem];



2008-01-16