Adding a Submenu to a Menu in OS X
Adding a Submenu to a Menu
A submenu, or hierarchical menu, is a menu-within-a-menu. When the cursor moves over the submenu name, a menu drops down and the user chooses an item from the submenu by moving the cursor over the item of interest and then releasing the mouse button.
A submenu it created in Interface Builder using the Submenu item from the palette. First bring the menu bar to the forefront and click the menu that's to receive the submenu. With the menu items displayed, drag the Submenu item from the palette and drop it on the menu. If you've missed your mark and the submenu ends up with an incorrect placement in the menu, just click it and drag and drop it at its proper position between the existing menu items. In Figure 1, a submenu it being added after the Beep Twice item in the Sound menu in the menu bar resource of a nib file.
Figure 1 - Adding a submenu to a menu.
Supply the submenu with a name by double-clicking the submenu and typing the new name. The new submenu comes with one item. You can rename it by double-clicking it and typing a name. To add another menu item, display the submenu contents by clicking the submenu name, clicking Item in the palette, and then dragging and dropping the item to the displayed submenu. In Figure 2, an item is being added beneath the Soft menu item in the Volume submenu.
Figure 2 - Adding a menu item to a submenu.
A menu item in a submenu is given a command in exactly the same way any other menu item is given a command: you make the item active and then enter the command in the item's Info window. In Figure 3, the Loud menu item in the Volume submenu is given the command vmHI (for volume high).
Figure 3 - Assigning a command to an item in a submenu.
The handling of a submenu item is the same as the handling of any other menu item. You just include event-handling code for the item's command. The next example program provides a demonstration of this.