Creating Cocoa Toolbars

How to add a custom toolbar to a cocoa application. You will learn how to create a custom toolbar item that hides the application when it is pressed.

This method requires Mac OS 10.5.

I assume you know how to: Use Xcode Use Interface Builder You can find the Xcode Project here

Create a new Cocoa Application project in Xcode

Open Xcode and start a new Cocoa Application Project

Name the Project ToolbarExample

Name the project and choose the project directory. Here I am using ToolbarExample as the project name and I keep my projects in a workspace folder.

Open the .nib file and add a toolbar

This creates an error, click on the red x to discover what the error is

This creates an error. Click on the little red x in the bottem right of the MainMenu.nib window for details.

Set deployment target to Mac OS 10.5

Set the deployment target to Mac OS X 10.5 and the error will disappear. Now save your .nib file.

Allow Toolbar to be customized

Return to Xcode and create the class ToolbarDelegate

Add Functions to ToolBarDelegate.h

#import <Cocoa/Cocoa.h>

@interface ToolbarDelegate : NSObject

// These three functions are delegate methods of NSToolbar.
- (NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag;
- (NSArray *) toolbarDefaultItemIdentifiers: (NSToolbar *) toolbar;
- (NSArray *) toolbarAllowedItemIdentifiers: (NSToolbar *) toolbar;

// The last function is for hiding the Application when the toolbar item is clicked.
- (void) _hideApplication: (id) sender;


Implementation of ToolbarDelegate.c Part 1

// These two delegates ADD the Hide toolbar item to the allowed toolbar items and the default toolbar items.
// Any toolbar items included in the .nib file will also be included.

- (NSArray *) toolbarDefaultItemIdentifiers: (NSToolbar *) toolbar;
    return [NSArray arrayWithObjects:    @"Hide", nil];

- (NSArray *) toolbarAllowedItemIdentifiers: (NSToolbar *) toolbar;
    return [NSArray arrayWithObjects:    @"Hide", nil];

Implementation of ToolbarDelegate.c Part 2

// This is the code that actually makes the ToolbarItem appear in the Toolbar.
// The image I am using is found in /Developer/Examples/AppleScript Studio/Images/AboutBox.tiff
// Just add the image to the project. (Or any other image you want.)
- (NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag;
    NSToolbarItem *item = [[NSToolbarItem alloc] initWithItemIdentifier:itemIdentifier];
    if ( [itemIdentifier isEqualToString:@"Hide"] )
        [item setLabel:@"Hide"];
        [item setPaletteLabel:@"Hide"];
        [item setImage:[NSImage imageNamed:@"AboutBox.tiff"]];
        [item setTarget:self];
        [item setAction:@selector(_hideApplication:)];
    return [item autorelease];

What the code means in a glance

Implementation of ToolbarDelegate.c Part 3

// This is called when the Hide toolbar item is clicked
- (void) _hideApplication: (id) sender ;
    [NSApp hide: self];

Add a ToolbarDelegate object in the .nib

Drag and NSObject into the MainMenu.nib window and change the class to ToolbarDelegate

Set the NSToolbar's delegate to ToolbarDelegate

Build and Go

Contratulations, You have created a custom toolbar item that hides the application when it is pressed.

Comments (0)

Add your comment

E-Mail me when someone replies to this comment