Selectable Toolbar Items

NSToolbar allows you to specify that certain items in the toolbar can indicate a selected state. This is often used in conjunction with an NSTabView that is configured to have no visible tabs. Figure 1 contains an example implementation similar to that of Safari and the Finder.

Figure 1  Selectable NSToolbar items used as preferences navigation
Selectable NSToolbar items used as preferences navigation

Toolbars that need to indicate item selection must specify the items that can be selected by implementing the delegate method toolbarSelectableItemIdentifiers:. This method returns an array containing the identifiers of the items that can be selected. The example Listing 1in returns all the identifiers for the preferences implementation.

Listing 1  Example implementation of toolbarSelectableItemIdentifiers:

- (NSArray *)toolbarSelectableItemIdentifiers: (NSToolbar *)toolbar;
{
    // Optional delegate method: Returns the identifiers of the subset of
    // toolbar items that are selectable. In our case, all of them
    return [NSArray arrayWithObjects:GeneralPreferences,
                                    AccountPreferences,
                                    AppearancePreferences,
                                    FontsAndColorsPreferences,
                                    AdvancedPreferences, nil];
}

Your application can specify the currently selected toolbar item using the method setSelectedItemIdentifier: passing the identifier for the desired toolbar item. The currently selected toolbar item is returned by the method selectedItemIdentifier. If there is no currently selected, nil is returned.