Toolbars

Cocoa implements toolbars in the NSToolbar and NSToolbarItem classes. NSToolbar implements the toolbar itself, while NSToolbarItem represents the individual icons and controls within the toolbar.

Warning

Unfortunately, as of the December 2002 release of the Developer Tools, toolbars cannot be created within Interface Builder. Instead, you have to create them manually with code.

To create a toolbar, instantiate an instance of NSToolbar and initialize it with the method initWithIdentifier:. The identifier in this method is a string that identifies the toolbar within an application (it is used, for example, in document-based applications to reflect changes in a toolbar in one document window in all the toolbars of all open document-windows). To attach a toolbar to a window, invoke NSWindow’s setToolbar:; the toolbar method of NSWindow returns a window’s assigned toolbar.

NSWindow provides a two action methods for interacting with toolbars. The method toggleToolbarShown: hides and shows the toolbar. This is the action for the “Hide Toolbar” menu item (as well as the pill-shaped button on the right side of the window’s title bar). Note that toggleToolbarShown: causes the title of this menu item to alternate between “Hide Toolbar” and “Show Toolbar”. This method relies on NSToolbar’s isVisible and setVisible: methods. isVisible returns YES if the toolbar is present, and NO otherwise; setVisible: takes a BOOL indicating whether the toolbar should be hidden or shown.

NSWindow ...

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.