Re: Getting a reference to an NSMenuItem from another nib?
Re: Getting a reference to an NSMenuItem from another nib?
- Subject: Re: Getting a reference to an NSMenuItem from another nib?
- From: Sherm Pendley <email@hidden>
- Date: Tue, 14 Jun 2005 20:59:15 -0400
On Jun 14, 2005, at 1:39 PM, Bill Bumgarner wrote:
You can also bind a menu item's "enabled" value to some particular
attribute of some object somewhere. Now, menu items appear in the
main NIB file and your document will typically have the controllers
bound through to the object model. But it isn't hard to add a
controller in the main menu that effectively provides a "gateway"
through to the current document's model.
This isn't completely elegant because of the location of the main
menu in the "other" nib, but it does work....
In your NSDocument subclass' +initialize method, you could define a
lookup table that maps menu selectors to property names:
static NSDictionary* menuValidationLookupTable;
+(void) initialize {
menuValidationLookupTable = [[NSDictionary alloc]
initWithObjectsAndKeys:
@"canDoThis", NSStringFromSelector(@selector(doThis:)),
@"canDoThat", NSStringFromSelector(@selector(doThat:)),
nil];
}
Then in -validateMenuItem: you simply get the selector and look up
the property:
-(BOOL) validateMenuItem: (NSMenuItem*)theItem {
NSString* selString = NSStringFromSelector([theItem action]);
NSNumber *val = [menuValidationLookupTable objectForKey:selString];
return (nil != val) ? [self valueForKey:[val boolValue]] : YES;
}
Naturally, you could still use bindings to connect these properties
with the states of various widgets in your document window. I think
trying to use bindings in the main menu nib might be more effort than
it's worth, though.
sherm--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden