Cocoa validateUserInterfaceItem and menu item hiding
Using valid开发者_如何学运维ateUserInterfaceItem it is possible to disable/enable a menu item but I need a similar behaviour to hide menu items, have you some hints?
- (BOOL)validateMenuItem:(NSMenuItem *)menuItem
Some details in the docs.
Update:
NSMenus do not hide NSMenuItems if validateMenuItem:
returns NO
. The according NSMenuItem gets disabled (Which tells the user that the functionality is there, but currently not applicable).
This behavior is also suggested in Apple's HIG. (See the second paragraph in the "Naming Menu Items" section)
Two other notes about NSMenuItem's enabled
state:
setEnabled
only works ifautoenablesItems:
of the hosting NSMenu is set toYES
- The default implementation of
validateMenuItem:
seems to traverse the responder chain to check if the target/action of a NSMenuItem is available.
You could use -(BOOL) validateMenuItem: to remove a menu item when it was unneeded and insert it when required.
See the Menu documentation
精彩评论