Toolbar code in separate Controller?
Toolbar code in separate Controller?
- Subject: Toolbar code in separate Controller?
- From: "John Love" <email@hidden>
- Date: Sat, 5 Jul 2008 15:05:30 -0400
Apple's SimpleToolbar code *is all* their MyDocument.m. My app would like
to place the toolbar-specific code *in a separate Controller*, say
"ToolbarController". This transfer does work to a limited extent, that is,
the toolbar does in fact appear at the top of the window, as it should.
However, *what does not work* is the enabling/disabling based on the
specified target .. all toolbar items are disabled. Details are as follows:
*Within MyDocument.h*:
@interface MedicalDocument:NSDocument {
IBOutlet ToolbarController *iboToolbarCtrl;
IBOutlet FileController *iboFileCtrl;
// etc.
}
*Within MyDocument.m*:
- (void) windowControllerDidLoadNib:(NSWindowController*)aController {
[super windowControllerDidLoadNib:aController];
// etc
[iboToolbarCtrl retainOutlets];
[iboToolbarCtrl setupToolbar];
// etc
}
*Within ToolbarController.h* – all of which is taken from SimpleToolbar
(except being in a separate Controller, versus all in MyDocument.m)
#import <Cocoa/Cocoa.h>
#import "FileController.h"
@interface ToolbarController:NSObject {
IBOutlet NSToolbarItem *openSpreadsheetItem, *saveSpreadsheetItem,
*stopCalculationItem;
IBOutlet NSObject *openItemOutlet, *saveItemOutlet,
*stopItemOutlet;
IBOutlet FileController *iboFileCtrl;
IBOutlet NSWindow *iboDocWindow;
}
- (void) dealloc;
- (void) retainOutlets;
- (void) setupToolbar;
- (NSToolbarItem*) toolbar:(NSToolbar*)toolbar
itemForItemIdentifier:(NSString*)itemIdent
willBeInsertedIntoToolbar:(BOOL) willBeInserted;
- (NSArray*) toolbarDefaultItemIdentifiers:(NSToolbar*)toolbar;
- (NSArray*) toolbarAllowedItemIdentifiers:(NSToolbar*)toolbar;
- (void) toolbarWillAddItem:(NSNotification*)notif;
- (void) toolbarDidRemoveItem:(NSNotification*)notif;
- (BOOL) validateToolbarItem:(NSToolbarItem*)toolbarItem;
- (BOOL) validateMenuItem:(NSMenuItem*)item;
@end
As stated at the start, the toolbar does in fact appear where it's supposed
to .. *it's just that all the items are disabled??*
Within *ToolbarController.m*, my setupToolbar method is identical to
Apple's, namely:
- (void) setupToolbar {
NSToolbar *toolbar = [[[NSToolbar alloc]
initWithIdentifier:MyDocToolbarIdentifier] autorelease];
[toolbar setAllowsUserCustomization:YES];
[toolbar setAutosavesConfiguration:YES];
[toolbar setDisplayMode:NSToolbarDisplayModeIconOnly];
[toolbar setDelegate:self];
[iboDocWindow setToolbar:toolbar];
}
My:
*-** (NSToolbarItem*) toolbar:(NSToolbar*)toolbar
itemForItemIdentifier:(NSString*)itemIdent
willBeInsertedIntoToolbar:(BOOL) willBeInserted;*
*except, I have such things as*:
[toolbarItem setTarget:iboFileCtrl];
[toolbarItem setAction:@selector(saveSpreadsheet:)];
My:
*- (void) toolbarWillAddItem:(NSNotification*)notif;*
*except, I have such things as*:
[openSpreadsheetItem setTarget:iboFileCtrl];
[openSpreadsheetItem setAction:@selector(openSpreadsheet:)];
since FileController.m contains the method openSpreadsheet
Further on:
*-** (BOOL) validateToolbarItem:(NSToolbarItem*)toolbarItem* {
BOOL enable = NO;
if ([[toolbarItem itemIdentifier] isEqual:OpenDocToolbarItemIdentifier])
{
enable = YES;
}
else if ([[toolbarItem itemIdentifier]
isEqual:SaveDocToolbarItemIdentifier])
{
if (![iboFileCtrl fileSaved]) enable = YES;
}
else if ([[toolbarItem itemIdentifier]
isEqual:StopDocToolbarItemIdentifier])
{
if (![iboFileCtrl fileFinishedCalculation]) enable = YES;
}
return enable;
}
And even later:
*- (void) toolbaritemclicked:(NSToolbarItem*)toolbarItem* {
if ([[toolbarItem itemIdentifier] isEqual:OpenDocToolbarItemIdentifier])
{
[iboFileCtrl openSpreadsheet];
}
else if ([[toolbarItem itemIdentifier]
isEqual:SaveDocToolbarItemIdentifier])
{
[iboFileCtrl saveSpreadsheet];
}
else if ([[toolbarItem itemIdentifier]
isEqual:StopDocToolbarItemIdentifier])
{
[iboFileCtrl stopCalculation];
}
}
Thanks to the patience of Graham Cox and others, I have learned a great deal
about targets and delegates .. so, I pray that my error does not involve
these concepts .. so I can at least say that my ignorance is on new ground.
John Love
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden