PDA

View Full Version : validateToolbarItem not being called




dgrenier
Feb 23, 2009, 08:12 PM
This one have been puzzled me for the last few days. I'm trying to implement a toolbar in my app, and whatever I do, my toolbar items stay disabled. I've nailed the problem to the fact that validateToolbarItem doesn't get called, even thought both items have (I think) a valid target/action pair.

I've red the documentations as well as did some research both here and on Google, but couldn't find anything helpful. Anybody got a clue?

Here's the code I've been using:

@implementation MainWindowController

NSString *const kProjectTitle = @"CustomProjectsView";
NSString *const kClientTitle = @"CustomClientsView";
static NSString* kProjectsToolbarItemIdentifier = @"Projects Toolbar Item Identifier";
static NSString* kClientsToolbarItemIdentifier = @"Clients Toolbar Item Identifier";

enum // popup tag choices
{
kProjectView = 0,
kClientView = 1,
};

- (void)awakeFromNib
{
NSLog(@"awakeFromNIB");
[self changeViewController: kClientView];

NSToolbar *toolbar = [[[NSToolbar alloc] initWithIdentifier:@"MainToolbar"] autorelease];

[toolbar setDelegate:self];
[toolbar setAllowsUserCustomization:NO];
[toolbar setAutosavesConfiguration:YES];

[mainWindow setToolbar:toolbar];
}

- (void)changeViewController:(NSInteger)whichViewTag
{
// Code for changing the view here
}

- (NSViewController*)viewController
{
NSLog(@"viewController");
return myCurrentViewController;
}

#pragma mark Toolbar Delegate Methods

- (NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag
{
NSLog(@"toolbar:itemForItemIdentifier:willBeInsertedIntoToolbar:");
NSToolbarItem *toolbarItem = nil;

if ([itemIdentifier isEqual: kProjectsToolbarItemIdentifier])
{
toolbarItem = [[[NSToolbarItem alloc] initWithItemIdentifier: itemIdentifier] autorelease];

[toolbarItem setLabel:@"Projets"];
[toolbarItem setPaletteLabel:@"Projets"];

[toolbarItem setToolTip:@"Affiche les projets"];
[toolbarItem setImage: [NSImage imageNamed: @"SaveDocumentItemImage"]];

[toolbarItem setTarget: self];
[toolbarItem setAction:@selector(changeViewController:kProjectView:)];
}
else if([itemIdentifier isEqual: kClientsToolbarItemIdentifier])
{
toolbarItem = [[[NSToolbarItem alloc] initWithItemIdentifier: itemIdentifier] autorelease];

[toolbarItem setLabel:@"Clients"];
[toolbarItem setPaletteLabel:@"Clients"];

[toolbarItem setToolTip:@"Affiche les clients"];
[toolbarItem setImage: [NSImage imageNamed: @"SaveDocumentItemImage"]];

[toolbarItem setTarget: self];
[toolbarItem setAction:@selector(changeViewController:kClientView:)];
}
else
{
toolbarItem = nil;
}

return toolbarItem;
}

- (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar *)toolbar
{
NSLog(@"toolbarAllowedItemIdentifiers:");
return [NSArray arrayWithObjects: kProjectsToolbarItemIdentifier, kClientsToolbarItemIdentifier, nil];
}

- (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar *)toolbar
{
NSLog(@"toolbarDefaultItemIdentifiers:");
return [NSArray arrayWithObjects: kProjectsToolbarItemIdentifier, kClientsToolbarItemIdentifier, nil];
}

-(BOOL)validateToolbarItem:(NSToolbarItem *)toolbarItem
{
NSLog(@"validateToolbarItem:");
return YES;
}

- (void) toolbarWillAddItem: (NSNotification *) notif {
NSLog(@"toolbarWillAddItem:");
}

@end



kainjow
Feb 23, 2009, 09:12 PM
You're setting up the selector incorrectly. Try changing it to:

- (void)changeViewController:(NSToolbarItem *)item
{
// Code for changing the view here
}

...

[toolbarItem setAction:@selector(changeViewController:)];

Set the tag on the toolbar item to one of your enum values and then check for it in the action method.

dgrenier
Feb 23, 2009, 10:55 PM
Ah, man, I don't know wether I should thank you for finding it so quickly, or just hate you because you pointed a so obvious mistake... :cool: