Cocoa validateUserInterfaceItem和菜单项隐藏

发布于 2024-10-01 09:16:32 字数 71 浏览 0 评论 0 原文

使用 validateUserInterfaceItem 可以禁用/启用菜单项,但我需要类似的行为来隐藏菜单项,您有一些提示吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千紇 2024-10-08 09:16:32

- (BOOL)validateMenuItem:(NSMenuItem *)menuItem

文档

更新:
如果 validateMenuItem: 返回 NO,NSMenus 不会隐藏 NSMenuItems。相应的 NSMenuItem 被禁用(这告诉用户该功能存在,但当前不适用)。
Apple 的 HIG。 (请参阅“命名菜单项”部分中的第二段)

关于 NSMenuItem 的 enabled 状态的另外两个注意事项:

  • setEnabled 仅在 autoenablesItems: 为托管 NSMenu 设置为 YES
  • validateMenuItem: 的默认实现似乎遍历响应者链以检查 NSMenuItem 的目标/操作是否可用。

- (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.
娇柔作态 2024-10-08 09:16:32

您可以使用 -(BOOL) validateMenuItem: 在不需要时删除菜单项并在需要时插入它。

请参阅菜单文档

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

See the Menu documentation

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文