NSMenu — Mac OS X 10.0


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, ...

Get Cocoa in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.