禁用/启用应用程序菜单项

发布于 2024-08-18 01:01:04 字数 227 浏览 10 评论 0原文

尝试学习菜单处理的基础知识。我的测试应用程序的菜单栏有 3 个菜单——即“TestApp”、“File”和“Help”。我发现我可以完全删除这些菜单,只需调用 say:

NSMenu* rootMenu = [NSApp mainMenu];
[rootMenu removeItemAtIndex:2];

但是,我只想暂时禁用它们(将它们灰显)。请问有同样简单的方法吗?

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?

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

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

发布评论

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

评论(1

宫墨修音 2024-08-25 01:01:04

我可能误解了你的问题,但似乎你希望能够使系统菜单栏上出现的菜单的实际标题变灰(例如使“文件”菜单变灰)。我不确定它是否可能,但它肯定违背了 Apple 人机界面指南

菜单标题不显示为灰色
即使所有菜单命令都是
同时不可用(变暗)。
用户应该始终能够查看
菜单的内容,无论它们是否
目前可用。

因此,问题的真正解决方案是当您的应用程序处于某种状态时,能够使某个菜单中的所有菜单项变灰。为此,实现 NSUserInterfaceValidations 协议。它只需要实现 - (BOOL)validateUserInterfaceItem: 方法。通常,在实现此方法时,您只需检查正在验证的用户界面项的选择器,如果应该启用则返回 YES,如果不应该则返回 NO(这将使菜单项变灰)。

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

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