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:
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:
Code:
@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