Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Nov 17, 2007, 07:29 PM   #1
Darkroom
Guest
 
Join Date: Dec 2006
Location: Montréal, Canada
coding for multiple toolbar items?

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
Darkroom is offline   0 Reply With Quote
Old Nov 18, 2007, 11:03 AM   #2
kainjow
Moderator emeritus
 
kainjow's Avatar
 
Join Date: Jun 2000
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.
kainjow is offline   0 Reply With Quote
Old Nov 18, 2007, 02:46 PM   #3
Darkroom
Thread Starter
Guest
 
Join Date: Dec 2006
Location: Montréal, Canada
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?

Last edited by Darkroom; Nov 18, 2007 at 02:52 PM.
Darkroom is offline   0 Reply With Quote
Old Nov 18, 2007, 09:14 PM   #4
Darkroom
Thread Starter
Guest
 
Join Date: Dec 2006
Location: Montréal, Canada
nevermind... i figured it out... typical

for future reference the linking code looks like this:

Code:
- (IBAction)selectColorTool:(id)sender
{
    setTarget:myColorTool;
	[myColorTool selectColor:nil];
}
Darkroom is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Items in Toolbar lukestrohbehn OS X 10.8 Mountain Lion 2 May 13, 2013 09:36 AM
How to delete MULTIPLE reading list items at the same time patent10021 OS X 0 Apr 9, 2013 02:13 AM
Running multiple actions on multiple items selection 1theo0 Mac Programming 1 Dec 21, 2012 08:44 AM
Not all items being tagged when tagging multiple items in iTunes willryan42 Mac Applications and Mac App Store 0 Oct 8, 2012 07:44 PM

Forum Jump

All times are GMT -5. The time now is 12:09 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC