Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Nov 17, 2007, 06: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, 10: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, 01: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 01:52 PM.
Darkroom is offline   0 Reply With Quote
Old Nov 18, 2007, 08: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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
One Time Machine Drive for Multiple Computers? wildwell Mac Applications and Mac App Store 6 Dec 31, 2010 12:26 PM
Help with c code for files coopf iPhone/iPad Programming 2 Nov 19, 2010 09:21 PM
Looking for drop-down toolbar item example code jpefjr Mac Programming 3 Jan 2, 2010 06:07 PM


All times are GMT -5. The time now is 04:50 AM.

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

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