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?
To enable and disable menu items, use the MenuItem class's setEnabled method.
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”.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With