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

Menus

The NSMenu and NSMenuItem classes implement menus in Cocoa. NSMenu provides an implementation for actual menus, while NSMenuItem represents individual items within menus. An application’s menus appear in the main menu bar across the top of the screen. By and large, an application’s main menu is assembled in Interface Builder, which provides facilities for editing menu structures and setting menu item targets and actions. Additionally, Interface Builder has several pre-configured menus, such as File and Edit that contain standard menu items familiar to users. An application’s main menu is contained in the main nib of an application.

Every application also has a Dock menu, which pops up when you right-click on an application’s Dock icon. Dock menus are easily created in Interface Builder by connecting an instance of NSMenu to the File’s Owner’s dockMenu outlet. Alternatively, the application delegate can supply a Dock menu by implementing the method applicationDockMenu: (this is useful if you want to dynamically reconfigure the menu). A third way of specifying a Dock menu is to assemble the NSMenu object in a nib and specify the nib’s file name in the application’s Info.plist file under the AppleDockMenu key.

NSView objects manage contextual menus. In Interface Builder, every view has a menu outlet, which can be connected to an NSMenu object. The menu that you connect to this outlet will appear as a contextual menu when you right-click over the view. Contextual menus can ...

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