Re: validateMenuItem in Cocoa Document Based App?
Re: validateMenuItem in Cocoa Document Based App?
- Subject: Re: validateMenuItem in Cocoa Document Based App?
- From: Joel Norvell <email@hidden>
- Date: Sun, 21 Jan 2007 00:47:41 -0800 (PST)
Matt R wrote:
> I have a WebView in MyDocument.nib and would like to enable/disable
> the Back/Forward Menu Item based on what canGoBack and canGoForward
> returns. How can I do this?
Matt,
I remember a general feeling of confusion when I addressed this problem several
months ago. I found Cocoa Programming to be helpful. There are pertinent
Apple docs too. I ended up looking at the Sketch example since it does add
menu items in a similar manner.
Your MainMenu.nib needs to contain a NSMenuItem for the WebView. Nested in
that you'll need a NSMenu and nested in that you'll need NSMenuItems for your
menu commands. Mine are NSMenuItem (Zoom In) and NSMenuItem (Zoom Out) In
your document subclass you'll want to add your menu commands and a
validateMenuItem method. Obviously you'll want to substitute your behavior for
mine. I hope that will be fairly straightforward.
Here are code snippets from my subclass of NSDocument:
- (void) zoomIn: (id) sender
{
float newScaleFactor = [self scaleFactor]* sqrt(sqrt(2.0));
[self setScaleFactor:newScaleFactor];
[myView doZoom:newScaleFactor];
[self resizeWindow];
}
- (void) zoomOut: (id) sender
{
float newScaleFactor = [self scaleFactor]/sqrt(sqrt(2.0));
[self setScaleFactor:newScaleFactor];
[myView doZoom:newScaleFactor];
[self resizeWindow];
}
- (BOOL) validateMenuItem: (NSMenuItem *) menuItem
{
BOOL enable = NO;
float kMaxScaleFactor = 2.0;
float kMinScaleFactor = 1.0;
if ([menuItem action] == @selector(zoomIn:))
{
if ([self scaleFactor] < (kMaxScaleFactor - 0.01))
{
enable = YES;
}
}
else if ([menuItem action] == @selector(zoomOut:))
{
if ([self scaleFactor] > (kMinScaleFactor + 0.01))
{
enable = YES;
}
}
else
{
NSLog(@"menuItem: %@", menuItem);
enable = [super validateMenuItem:menuItem]; // THIS HAS TO BE HERE OR
NOTHING HAPPENS.
}
return enable;
}
My recipe is a bit sketchy, but I hope it helps you.
Joel
____________________________________________________________________________________
Food fight? Enjoy some healthy debate
in the Yahoo! Answers Food & Drink Q&A.
http://answers.yahoo.com/dir/?link=list&sid=396545367
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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