Toolbar Style?

Discussion in 'Mac Programming' started by Darkroom, Dec 11, 2008.

  1. Darkroom Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #1
    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.
     

    Attached Files:

  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    Implement the toolbarSelectableItemIdentifiers: delegate method.

    Once implemented you can select items via setSelectedItemIdentifier:
     
  3. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #3
    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?
     
  4. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #4
    Hm. Well first off without setting the selected item manually, are you getting the desired effect?
     
  5. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #5
    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.
     
  6. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #6
    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.
     
  7. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #7
    so identifying them by their label doesn't work, should i identify them by their tag instead? how is that done?
     
  8. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #8
    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.
     
  9. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #9
    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:
     
  10. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #10
    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]];
     
  11. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #11
    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.
    
     
  12. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #12
    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.
     
  13. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #13
    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.
     

Share This Page