• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Getting a reference to an NSMenuItem from another nib?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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 21:11:22 -0400

On Jun 14, 2005, at 8:59 PM, Sherm Pendley wrote:

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:

Meh. Night of the Living Typos. :-(

-(BOOL) validateMenuItem: (NSMenuItem*)theItem {
NSString* selString = NSStringFromSelector([theItem action]);
NSNumber *val = [menuValidationLookupTable objectForKey:selString];

// Typo #1. Should be:
NSString *val = [menuValidationLookupTable objectForKey:selString];


return (nil != val) ? [self valueForKey:[val boolValue]] : YES;

// Typo #2. That should be: 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


References: 
 >Re: Getting a reference to an NSMenuItem from another nib? (From: "Theodore H. Smith" <email@hidden>)
 >Re: Getting a reference to an NSMenuItem from another nib? (From: Sherm Pendley <email@hidden>)
 >Re: Getting a reference to an NSMenuItem from another nib? (From: Andy Lee <email@hidden>)
 >Re: Getting a reference to an NSMenuItem from another nib? (From: Bill Bumgarner <email@hidden>)
 >Re: Getting a reference to an NSMenuItem from another nib? (From: Sherm Pendley <email@hidden>)

  • Prev by Date: Re: Representing hierarchy in NSPopupButtonCell
  • Next by Date: Re: xcode-->java-->cocoa - how long? how many?
  • Previous by thread: Re: Getting a reference to an NSMenuItem from another nib?
  • Next by thread: Re: Getting a reference to an NSMenuItem from another nib?
  • Index(es):
    • Date
    • Thread