NSMenuItem Status Enabled, Dynamic Naming, and Target Action

I have a problem with my application menu. I want several menu items to be grayed out depending on some BOOL variables.

In addition, I want some of my menu items to get names depending on some BOOL variables, and thereby perform different functions depending on what the name of the menu item is. Is it possible? Using Interface Builder, you can associate a menu item with one IBAction method. please tell me how to do it.

thanks

+4
source share
3 answers

See NSMenuValidation Protocol .

You implement -validateMenuItem:, which is used to determine whether a menu item should be enabled or disabled. He called up each menu item just before the menu appeared.

+4
source

Yes. You can configure the status of the menu item and its name programmatically; see NSMenuItem documentation. Remember to use NSLocalizedString when getting the header format.

+1
source

The second question; you do not need to use Interface Builder to enable Target / action for menu items.

You can use this code:

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

All Articles