Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Darkroom

Guest
Original poster
Dec 15, 2006
2,445
0
Montréal, Canada
i have a toolbar that validates the toolbar items by tag:

Code:
- (BOOL)validateToolbarItem:(NSToolbarItem *)item
	{
	if ([item tag] == currentViewTag)
		{
		return NO;
		}
		else
		{
		return YES;
		}
	}

but i much prefer the more standard "pushed" look (or whatever) of selected toolbar items. what method do i enter to have this look? see attachment to see the look i'm talking about.
 

Attachments

  • ToolBarStyle.jpg
    ToolBarStyle.jpg
    7.6 KB · Views: 242

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
Implement the toolbarSelectableItemIdentifiers: delegate method.

Once implemented you can select items via setSelectedItemIdentifier:
 

Darkroom

Guest
Original poster
Dec 15, 2006
2,445
0
Montréal, Canada
Implement the toolbarSelectableItemIdentifiers: delegate method.

Once implemented you can select items via setSelectedItemIdentifier:

i'm not getting it... i have a tool bar with 3 items labeled "Preferences", "About" and "Help". so in my NSWindow class which hosts the toolbar i've added:

Code:
- (NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar;
	{
	return [NSArray arrayWithObjects:@"Preferences", @"About", @"Help", nil];
	}

then for each IBAction attached to a these toolbar items that switches in the appropriate view, i've added this:

Code:
- (IBAction)selectPreferences:(id)sender
	{
	[[self toolbar] setSelectedItemIdentifier:@"Preferences"];
        ...
        }

what am i doing wrong?
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
Hm. Well first off without setting the selected item manually, are you getting the desired effect?
 

Darkroom

Guest
Original poster
Dec 15, 2006
2,445
0
Montréal, Canada
if i only add

Code:
- (NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar;
	{
	return [NSArray arrayWithObjects:@"Preferences", @"About", @"Help", nil];
	}

and do not include "[[self toolbar] setSelectedItemIdentifier:mad:"Preferences"];" to my selectPreferences IBAction it doesn't work either.
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
Are you using the exact same identifiers for the other toolbar methods? BTW I wouldn't use the strings directly. Instead #define them, or declare as variables, so you don't risk using the wrong strings.
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
No you need to use their identifiers. Did you setup the toolbar in IB? If so you need to set identifiers for your items if you haven't. Also if you set it up in IB you need to set the delegate to your controller.
 

Darkroom

Guest
Original poster
Dec 15, 2006
2,445
0
Montréal, Canada
No you need to use their identifiers. Did you setup the toolbar in IB? If so you need to set identifiers for your items if you haven't. Also if you set it up in IB you need to set the delegate to your controller.

humm... i thought it was strange only having to list their labels... but how do i set their identifier in IB? do i have to make IBOutlets to each Toolbar Item and use that as their identifiers?

when selecting the toolbar items in IB, i don't see any place in the inspector to set their identifier... i only see Label, Pal. Label, Tag and Priority... :confused:
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
I just checked it out myself. I guess you can't set the identifier in IB, which is dumb. If all items in your toolbar can be selectable, than you could try this (not tested):
Code:
- (NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar {
    return [[toolbar items] valueForKey:@"identifier"];
}
This gets all the toolbar items, then returns an array of each object's identifier.

To work with setSelectedItemIdentifier: you could set up IBOutlets for each NSToolbarItem in IB and then use:
Code:
[toolbar setSelectedItemIdentifier:[myItem identifier]];
 

Darkroom

Guest
Original poster
Dec 15, 2006
2,445
0
Montréal, Canada
doesn't seem to work. i've set the toolbar delegate to the window class, and connected the toolbar items with outlets like "IBOutlet NSToolbarItem *preferencesItem;"

with attempting to set the identifier in the IBAction, the compiler warns:
Code:
NSToolbarItem may not respond to identifer

then when choosing the different items in the toolbar, the debugger reads:
Code:
[<NSToolbarFlexibleSpaceItem 0x1305c0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key identifier.
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
Oops try itemIdentifier instead. hm since you don't want to select flexible items and separators, you may need to write a method that returns the items you want by tag. Or just setup IBOutlets for each item you want selectable, then create an array from that. Might be the easiest way.
 

Darkroom

Guest
Original poster
Dec 15, 2006
2,445
0
Montréal, Canada
Code:
- (NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar {
    return [[toolbar items] valueForKey:@"itemIdentifier"];
}

Code:
[toolbar setSelectedItemIdentifier:[myItem itemIdentifier]];

this worked... :)

wow, for something that i totally assumed was automatic, it sure takes a lot of effort to implement this small detail.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.