selectable NSToolbarItems and itemIdentifiers in Leopard
Maybe it’s just me, but I couldn’t figure out how to specify an itemIdentifier for a toolbar item in Interface Builder 3.0 (OS X 10.5 Leopard). So I didn’t know what to return from the toolbar’s delegate method toolbarSelectableItemIdentifiers to flag items as selectable. Nor could I just flag an item as selectable directly in IB. The default identifiers given to the items are GUIDs and completely meaningless. I’m guessing these are just oversights in the newly-minted IB. (Prior to Leopard you couldn’t define toolbars in IB at all.)
I also didn’t google any obvious hits on this topic, so here’s my community contribution for today. Specify view tags on your toolbar items in IB instead of identifiers. Then implement toolbarSelectableItemIdentifiers in your delegate to check for the tags you want.
- (NSArray*) toolbarSelectableItemIdentifiers: (NSToolbar *)toolbar
{
// yarr. leopard be toyin' with me, yarr!
static NSMutableArray* identifiers = nil;
if (identifiers == nil) {
NSArray* items = [toolbar items];
identifiers = [[NSMutableArray alloc] init];
for(NSToolbarItem* item in items) {
if ([item tag] == 1 || [item tag] == 2) {
[identifiers addObject:[item itemIdentifier]];
}
}
}
return identifiers;
}
I happened to have two toolbar items I wanted to be selectable, with tags 1 and 2. Modify to suit your needs …
The pirate-speak comment is of course critical to this method’s success.
February 24th, 2008 at 2:06 pm
If you don’t have unique tags, you could also have outlets to the toolbar items.