每次打开时修改 NSMenu 结构?
我需要能够在每次显示时动态修改 NSMenu 层次结构(添加/删除项目等)。例如:
- 用户在主菜单上启动跟踪会话并选择一个子菜单
- 检测子菜单即将打开并运行代码以修改它
- 继续跟踪,用户再次跟踪同一子菜单:goto 2
因此,为了做到这一点,我有一个对象实现NSMenuDelegate 协议。方法 menuNeedsUpdate 第一次工作(2),但第二次打开子菜单时不起作用。 (每个跟踪会话仅调用一次)
方法 menuWillOpen 每次都会被调用,但文档有以下警告,这似乎不符合使用此方法的资格:
不要修改结构 在此期间的菜单或菜单项 方法。
有什么办法可以做到这一点吗?
I need to be able to dynamically modify an NSMenu hierarchy each time it is shown (add/remove items etc). For example:
- user starts a tracking session on a main menu and selects a submenu
- detect submenu is about to open and run code to modify it
- keep tracking , user tracks over the same submenu again: goto 2
So to do this I have an object implementing the NSMenuDelegate protocol. The method menuNeedsUpdate works the first time (2), but does not work for 2nd time the submenu is opened. (Only called once per tracking session)
The method menuWillOpen is called each time, but has docs have the following warning which seems to disqualify using this approach:
Do not modify the structure of the
menu or the menu items during this
method.
Is there any way to accomplish this ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以子类化 NSMenu 并覆盖 子菜单操作:。
或者您可以订阅 NSMenuWillSendActionNotification。
虽然听起来不太适合您,但仅供参考, NSMenuValidation 是逐项更新菜单项的好地方。
You could subclass NSMenu and override submenuAction:.
Or you could just subscribe to the NSMenuWillSendActionNotification.
And while doesn't sound like it will work for you, just for reference, NSMenuValidation is a good place to update menu items on an item by item basis.
menuWillOpen 只会在您第一次跟踪子菜单时被调用一次。此时,您将填充菜单。
之后,menuWillOpen将不会再次被调用。但是,菜单的任何更改都将实时进行。因此,当主父菜单打开时,只要源数据发生更改(或者如果无法检测到更改,则定期更改),请使用正常的 NSMenu API 更新菜单。
确保您用来更新菜单的任何方法都会在系统跟踪您的菜单时运行。
menuWillOpen will only be called once, the first time you track over the submenu. At that point, you populate the menu.
After that, menuWillOpen will not be called again. However, any changes to the menu will happen live. So while the main parent menu is open, whenever the source data changes (or periodically if you can't detect changes), update the menu using the normal NSMenu API.
Make sure whatever method you use to update the menu will run while the system is tracking your menu.