King
King

Reputation: 239

NSMenuItem enabled state, dynamic naming and target action

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

Upvotes: 4

Views: 2497

Answers (3)

Abizern
Abizern

Reputation: 150685

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")]; 

Upvotes: 0

Peter Hosey
Peter Hosey

Reputation: 96363

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.

Upvotes: 1

Darren
Darren

Reputation: 25619

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.

Upvotes: 4

Related Questions