tags:

views:

460

answers:

4

Hi Guys

I am having a problem with my app's menu. I want a few items in the menu to be greyed out depending on some BOOL variables.

Also, I want some of my menu items to get names depending on some BOOL variables and thereby execute different functions depending on what the menu item name is. Is this possible? As using Interface Builder you can link a menu item to one IBAction method. please tell me how to do this.

Thanks

+2  A: 

See the NSMenuValidation protocol.

You implement -validateMenuItem:, which is used to determine whether a menu item should be enabled or disabled. It's called for each menu item just before a menu is popped up.

Darren
Thank you Darren
King
+1  A: 

Yes. You can set a menu item's enabled state and its title programmatically; see the NSMenuItem documentation. Remember to use NSLocalizedString when obtaining the title format.

Peter Hosey
Thanks you Peter.
King
A: 

For the second question; you don't need to use Interface Builder to hook up Target/action for Menu items.

You can use code such as this:

NSMenuItem *menuItem; // Set this to your menu item.
// Set the target to an instance of a class which contains the action method.
[menuItem setTarget:targetClass]; 
// Set the action to the (IBAction) method to call.
[menuItem setAction:NSSelectorFromString(@"actionMethod")];
Abizern
Er, `target` should generally be an instance, not a class. It can be a class, but that's not usually what you want. (Also, you forgot a ‘`)`’.)
Peter Hosey
Thanks Peter. Corrected.
Abizern
Thank you Abizern.
King
Shorthand: @selector(actionMethod:)
Pierre Bernard
A: 

Darren, you said: "It's called for each menu item just before a menu is popped up". Well, i've tested it, and I have to say that it's not true. Just some of the menu items are processed by the -validateMenuItem method... don't know why, but it gave me just trouble using it!

Woofy
I tested the validate method and this is my observation. I think its called only for menu items which are enabled.
King
well, not on my mac :P
Woofy
Woofy check the connections between application menu items and IBAction methods in interface builder. Coz validate only checks application menu items that are properly connected.
King