Build a toolbar in Interface Builder
Interface Builder 3 (IB3), part of the development tools that are shipped with Mac OS X Leopard, finally allows building an application window toolbar. Until the release of IB3 the setup had to be done entirely in code, or through a 3rd party extension to IB. Allthough the toolbar in the application I’m working on was working fine, I couldn’t resist the temptation to move its definition into my NIB file and clean up my code a bit.
In the objects library, under Application there is a new set of objects filed as Toolbar. You can drop a toolbar object onto a window which will give you a standard implementation of the toolbar, with icons for Show colors, Show fonts, Print and Customize. Select the toolbar to change its default properties in the Attribute Inspector window. Note that a new toolbar has the Customizable checkbox unchecked, meaning that a user of you application can’t customize the toolbar. Simply check it to change this behaviour.
To change the toolbar, you need to click on it in the window where you dropped it, which shows a panel containing the allowed toolbar items.
From the object library you can add toolbar items, either one of the standard items or an image item. Alternatively you can drop an image directly from the media library which will create an image item. On the Attribute Inspector window you can change the attributes for an image toolbar item (on Toolbar Item Attributes), and add a Tooltip (on Toolbar Item Identity). To change the standard toolbar that will be active when the application is started, drag items from the panel onto the toolbar, or drag items you don’t want out of the toolbar.
The standard items will have default activities attached to them, for the image items you added yourself you will have to add an action by Control-dragging from the item to an object (usually FirstResponder or File’s Owner) and selecting the action you want executed when the item is clicked.
This is all really easy, as long as you keep in mind that attributes and actions are specified on the items in the panel with the allowed items, rather on the items in the toolbar itself. While most of the configuration can now be done in IB, you still need to implement the method:
- (BOOL)validateToolbarItem:(NSToolbarItem *)theItem
in the class that is the delegate for the toolbar. In the implementation of the method you need to check for which item it was called, and return YES or NO depending on whether you want the item to be active (clickable) or not. You set tag values for the different items in IB against which you can check for which item the validateToolbarItem method is called. A sample implementation for a save button in a document based application could look like this:
- (BOOL)validateToolbarItem:(NSToolbarItem *)theItem
{
if ([theItem tag] == 1001) // Save toolbar item
return [self isDocumentEdited];
return YES;
}
One thing to note: if you have put a toolbar into your NIB file, you will need to save the file in either XIB3.x or NIB3.x format. Saving in NIB2.x format is not supported, so you lose backwards compatibility with IB2.
Tom:
thanks man,
really interesting and useful!
tom
28 September 2008, 11:16 pmMike Rundle:
YES! Exactly what I was looking for!
I couldn’t understand why the toolbar items I created in IB were always showing as greyed-out when I ran my application and now I know why. You are my hero for today!
16 October 2008, 11:32 pm