O'Reilly logo

Cocoa in a Nutshell by James Duncan Davidson, Michael Beam

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Name

NSMenu — Mac OS X 10.0

Synopsis

This class provides an interface to an application’s menus. Generally, you won’t have to work directly with menus since Interface Builder provides facilities for creating an application’s entire menu structure, including the main menu bar, the Dock menu, contextual menus for views, and menus for pop-up buttons. However, if your application requires some degree of dynamicism in its menus, then you will need to use NSMenu’s API.

Menus are initialized with initWithTitle:. The string passed in this method appears as the title of the menu. A menu manages a collection of menu items, which are instances of the class NSMenuItem. To manage a menu’s items, we have several methods at our disposal. To add an item to the end of the menu, use addItem:; to insert an item at some position in the menu, use insertItem:atIndex:. Menu items are removed from a menu using the methods removeItem: and removeItemAtIndex:.

Menus can also be queried for their menu items. The method itemArray returns an NSArray containing the menu’s items. Items can also be retrieved by their index, title, and tag using itemAtIndex:, itemWithTitle:, and itemWithTag:, respectively.

To add a submenu to a menu, first add a menu item to represent that submenu, and then associate another instance of NSMenu with that menu item using the method setSubmenu:forItem:.

image with no caption

@interface NSMenu : NSObject <NSCoding, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required