coding for multiple toolbar items?

Discussion in 'Mac Programming' started by Darkroom, Nov 17, 2007.

  1. Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #1
    hi there... it seems i'm not writing something correctly in this code... i know my IBActions aren't included yet, but nothing is showing up in the window's toolbar at all (even thought the tool bar is present)... i think the problem is combining the items for the proper syntax, but as of now i'm totally lost...

    Code:
    #import <Cocoa/Cocoa.h>
    
    @interface ToolBar : NSObject {
    	IBOutlet id mainWindow2;
    	NSToolbarItem *ColorItem;
    	NSToolbarItem *WhiteItem;
    	NSToolbarItem *FullScreenItem;
    	NSToolbar *toolbar;
    }
    
    - (void)setupToolbar;
    
    
    @end
    
    
    ----------------------------------
    
    
    #import "ToolBar.h"
    
    @implementation ToolBar
    
    - (void)awakeFromNib
    {
         ColorItem = [[NSToolbarItem alloc] initWithItemIdentifier:@"Color"];
        [ColorItem setLabel:NSLocalizedString(@"Color", nil)];
        [ColorItem setToolTip:@"Select Light Color"];
        [ColorItem setPaletteLabel:[ColorItem label]];
        [ColorItem setImage:[NSImage imageNamed:@"ColorLightToolBar.png"]];
        [ColorItem setTarget:self];
        [ColorItem setAction:@selector(selectColor:)];
    
    	 WhiteItem = [[NSToolbarItem alloc] initWithItemIdentifier:@"White"];
        [WhiteItem setLabel:NSLocalizedString(@"Color", nil)];
        [WhiteItem setToolTip:@"Select Default White Light"];
        [WhiteItem setPaletteLabel:[WhiteItem label]];
        [WhiteItem setImage:[NSImage imageNamed:@"WhiteLightToolBar.png"]];
        [WhiteItem setTarget:self];
        [WhiteItem setAction:@selector(selectWhite:)];
    
    	 FullScreenItem = [[NSToolbarItem alloc] initWithItemIdentifier:@"Full Screen"];
        [FullScreenItem setLabel:NSLocalizedString(@"Color", nil)];
        [FullScreenItem setToolTip:@"Enter Full Screen Mode"];
        [FullScreenItem setPaletteLabel:[FullScreenItem label]];
        [FullScreenItem setImage:[NSImage imageNamed:@"FullScreenToolBar.png"]];
        [FullScreenItem setTarget:self];
        [FullScreenItem setAction:@selector(selectFullScreen:)];
    
        [self setupToolbar];
    }
    
    - (NSToolbarItem *)toolbar:(NSToolbar *)toolbar
       itemForItemIdentifier:(NSString *)itemIdentifier
       willBeInsertedIntoToolbar:(BOOL)flag
    	{
        NSToolbarItem *item = [[NSToolbarItem alloc] initWithItemIdentifier:itemIdentifier];
    
        if ([itemIdentifier isEqualToString:@"Color"] && [itemIdentifier isEqualToString:@"White"] && [itemIdentifier isEqualToString:@"Full Screen"])
        {
            return ColorItem, WhiteItem, FullScreenItem;
        }
        return [item autorelease];
    }
    
    - (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar*)toolbar
    {
        return [NSArray arrayWithObjects:NSToolbarSeparatorItemIdentifier,
    			NSToolbarSpaceItemIdentifier,
    			NSToolbarFlexibleSpaceItemIdentifier,
    			NSToolbarCustomizeToolbarItemIdentifier, @"Color", @"White", @"Full Screen", nil];
    }
    
    - (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar*)toolbar
    {
        return [NSArray arrayWithObjects:@"Color", @"White", @"Full Screen", nil];
    }
    
    - (void)setupToolbar
    {
        toolbar = [[NSToolbar alloc] initWithIdentifier:@"Toolbar"];
        [toolbar setDelegate:self];
        [mainWindow2 setToolbar:[toolbar autorelease]];
    }
    
    - (IBAction)selectColor:(id)sender
    {
    
    }
    
    - (IBAction)selectWhite:(id)sender
    {
    
    }
    
    - (IBAction)selectFullScreen:(id)sender
    {
    
    }
    
    @end
    
     
  2. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    Code:
    if ([itemIdentifier isEqualToString:@"Color"] && [itemIdentifier isEqualToString:@"White"] && [itemIdentifier isEqualToString:@"Full Screen"])
        {
            return ColorItem, WhiteItem, FullScreenItem;
        }
    This is incorrect syntax. Plus the if statement will never be true. Maybe something like this will work:

    Code:
    if ([itemIdentifier isEqualToString:@"Color"])
        return ColorItem;
    if ([itemIdentifier isEqualToString:@"White"])
        return WhiteItem;
    if ([itemIdentifier isEqualToString:@"Full Screen"])
        return FullScreenItem;
    And there's no need to initialize a new NSToolbarItem if you're going to return a different object. You're just leaking memory.
     
  3. thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #3
    hi... yeah i figured out the syntax a few hours ago but forgot about this thread... its too bad i'm unable to delete threads i start as it seems i often end up solving problems after seeking help... and yes, what you suggested is what works...

    but since the thread exists:

    i'm trying to link the Toolbar IBActions to IBActions that already exist in other objects... for example, there already exists an IBAction called "colorSelector" in my colorController.m... so i tried adding an include in the windowToolbar.m

    Code:
    #include "colorController.h"
    
    and writing the IBAction in the windowToolbar.m like this:

    Code:
    - (IBAction)selectColor:(id)sender
        {
        setAction:@selector(colorSelector:);
        }
    
    and of course it doesn't work out... essentially the toolbar actions should do the same thing as some of the items in the main menubar, but i just don't know how to link them correctly.

    any thoughts?
     
  4. thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #4
    nevermind... i figured it out... typical :p

    for future reference the linking code looks like this:

    Code:
    - (IBAction)selectColorTool:(id)sender
    {
        setTarget:myColorTool;
    	[myColorTool selectColor:nil];
    }
    
     

Share This Page