advertisement

Article:
  Creating Toolbars for Mac OS X
Subject:   Setting toolbars
Date:   2002-03-20 13:00:38
From:   joaquimlopes
I always read the Programming With Cocoa articles with interest and have learned a few things with them. However I find the method for setting the toolbars that was presented to be very convoluted (too many ifs). For my current project, a students management application, I came up with what I believe is a simpler way. Here it is:


- (NSToolbarItem *)toolbar: (NSToolbar *)toolbar itemForItemIdentifier: (NSString *)itemIdent willBeInsertedIntoToolbar:(BOOL) willBeInserted
{
// Required delegate method Given an item identifier, self method returns an item
// The toolbar will use self method to obtain toolbar items that can be displayed in the customization sheet, or in the toolbar itself
NSToolbarItem *toolbarItem = [[[NSToolbarItem alloc] initWithItemIdentifier: itemIdent] autorelease];
/* The MEDocumentManager is an object that I created that does many things, in one of them it reads a plist
called Ferramentas (the portuguese word for tools) that is a dictionary of dictionaries where the item
identifier for tool is the key, like this (this is an example for a tool called Actualizar that uses an image
called - what else - Actualizar, and has a Tag - 9 -):
<dict>
<key>Actualizar</key>
<dict>
<key>Image</key>
<string>Actualizar</string>
<key>Label</key>
<string>Actualizar</string>
<key>Tag</key>
<integer>9</integer>
<key>Tooltip</key>
<string>Actualiza os dados</string>
</dict>
</dict>
*/
NSDictionary *tools = [[MEDocumentManager sharedDocumentManager] toolsDictionary: itemIdent];


if(tools == nil) // Just checking, for sanity sake
{
toolbarItem = nil;
}
else
{
// Set the text label to be displayed in the toolbar and customization palette
[toolbarItem setLabel: [tools objectForKey:@"Label"]];
[toolbarItem setPaletteLabel: [tools objectForKey:@"Label"]];


// Set up a reasonable tooltip, and image
[toolbarItem setToolTip: [tools objectForKey:@"Tooltip"]];
[toolbarItem setImage: [NSImage imageNamed: [tools objectForKey:@"Image"]]];


// Tell the item what message to send when it is clicked
[toolbarItem setTarget: self];
[toolbarItem setAction: @selector(toolbarItemAction:)];
[toolbarItem setTag: [[tools objectForKey:@"Tag"] intValue]];
}
return toolbarItem;
}


The toolbarItemAction method then just looks like this:
- (void)toolbarItemAction: (id)item
{
switch([item tag])
{
case whateverTag:
[self doWhatever];
break;
// Other cases...
}
}


And the validateToolbarItem method looks like this


- (BOOL) validateToolbarItem: (NSToolbarItem *) toolbarItem
{
// Optional method self message is sent to us since we are the target of some toolbar item actions
// (for example: of the save items action)
BOOL enable = NO;

switch([toolbarItem tag])
{
case whateverTag:
enable = YES;
break;
// Other cases...
}
return enable;
}


I hope someone finds this helpful. At least for me this has saved me a lot of time and has enabled me to change a few things without changing a line of code.


Joaquim


1 to 1 of 1
  1. Michael Beam photo Setting toolbars
    2002-03-25 17:36:12  Michael Beam | O'Reilly Author [View]

    • Setting toolbars
      2006-07-27 04:54:49  sajid_hayat [View]

1 to 1 of 1