Search code examples
cocoamenunsmenu

Cocoa validateUserInterfaceItem and menu item hiding


Using validateUserInterfaceItem it is possible to disable/enable a menu item but I need a similar behaviour to hide menu items, have you some hints?


Solution

  • - (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 if autoenablesItems: of the hosting NSMenu is set to YES
    • The default implementation of validateMenuItem: seems to traverse the responder chain to check if the target/action of a NSMenuItem is available.