Apple's code in link provided by robbieduncan seems rather overly complicated for such a relatively simple thing as toolbar. Heres some sample code of the method that I've used in some of my own applications.
Code:
// General preferences
NSToolbarItem *general = [[NSToolbarItem alloc] initWithItemIdentifier:@"General"];
[general setLabel:@"General"];
[general setImage:[NSImage imageNamed:@"GeneralPreferences"]];
[general setTarget:self];
[general setAction:@selector(displayGeneralPreferences:)];
Basically you need to create the above block of code for each of your toolbar items, obviously changing the appropriate fields. Once you've created each of the items you then need to create an instance of
NSToolbar to attach to your window. This can be done using:
Code:
NSToolbar *toolbar = [[[NSToolbar alloc] initWithIdentifier:@"preferenceToolbar"] autorelease];
[toolbar setDelegate:self];
[toolbar setSelectedItemIdentifier:@"General"];
[[self window] setToolbar:toolbar];
Once you've created all the above code I usually put it all in one method, for example
- (void)setupToolbar and then call it from your
awakeFromNib method so it dispalys when the window appears on screen.
Remember you
must implement the toolbars delegate methods in the delegate you specified above. The methods are:
Code:
- (NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag;
- (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar*)toolbar;
- (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar*)toolbar;
- (NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar;
The last 3 methods just return an array of your toolbar item identifiers.
It looks more complicated than it actually is but if you keep researching it you'll eventually get it.