Pergunta

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?

Foi útil?

Solução

- (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.

Outras dicas

You could use -(BOOL) validateMenuItem: to remove a menu item when it was unneeded and insert it when required.

See the Menu documentation

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top