Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disabling/enabling an application menu item

In trying to learn the very fundamentals of menu handling. My test app's menubar has 3 menus -- namely "TestApp", "File" and "Help". I find I can remove these menus entirely, simply by calling say:

NSMenu* rootMenu = [NSApp mainMenu];
[rootMenu removeItemAtIndex:2];

However, I'd only ever want to temporarily disable them (gray them out). Is there an equally simple way to do this, please?

like image 524
Bender Avatar asked Jan 18 '10 06:01

Bender


People also ask

Which of the following method is used to enabling and disabling menu items?

To enable and disable menu items, use the MenuItem class's setEnabled method.

How do I disable menu items?

The MenuItem class contains a property named visible (boolean), which specifies whether to display the current MenuItem. You can set the value to this property using the setVisible() method. To disable a particular menu item invoke the setVisible() method on its object by passing the boolean value “false”.


1 Answers

I may be misunderstanding your question, but it seems like you want to be able to gray-out the actual titles of menus that appear with the system's menu bar (Such as graying-out the "File" menu). I'm not sure if it's even possible, but it certainly goes against the Apple Human Interface Guidelines:

A menu’s title is displayed undimmed even if all of the menu’s commands are unavailable (dimmed) at the same time. Users should always be able to view a menu’s contents, whether or not they are currently available.

So, the real solution to the problem is to be able to gray-out all of the menu items within a certain menu when your application is in a certain state. To do this, implement the NSUserInterfaceValidations protocol. It only requires implementing the - (BOOL)validateUserInterfaceItem: method. Typically, when implementing this method, you simply check the selector of the user interface item being validated, and return YES if it should be enabled, or NO if it should not (which will gray-out the menu item).

like image 181
CJ. Avatar answered Sep 18 '22 06:09

CJ.