如何验证通过 Cocoa Bindings 动态填充的菜单项
背景
我正在通过 Cocoa Bindings 填充 TableView 的 NSPopupMenuCell
菜单。一切都很好(我已经绑定了“内容”、“内容值”和“选定的对象”)。菜单在运行时正确填充,并且底层变量已正确设置(通过 KVC)。一切都是为了这个目的。
我的问题
接下来我想控制填充的菜单项的状态(因为有些菜单项需要禁用)。现在我已经阅读了Apple关于 启用菜单的文档项目非常仔细......但对于我的一生,我似乎无法实现 validateMenuItem:
也无法实现 validateUserInterfaceItem:
其中他们实际上会被打电话。
除其他外,我已经尝试在现有的应用程序委托中实现这些方法,但没有任何乐趣。我是否可能误解了急救人员链?
另外,从逻辑上讲,另一种可能性是菜单项实际上确实有目标(上面假设它们没有)。但是,我似乎无法识别他们的目标(以便我可以在那里实现 validateMenuItem: )。
The Context
I'm populating a TableView's NSPopupMenuCell
's menu via Cocoa Bindings. All is well (I've bound 'Content', 'Content Values' and 'Selected Objects'). The menu is correctly populated at runtime and the underlying variable is properly being set (via KVC). Everything works on that end.
My Problem
Next I'd like to control the state of the populated menu items (as some would need to be disabled). Now I've read Apple's documentation on Enabling Menu Items very carefully… but for the life of me I can't seem to implement validateMenuItem:
nor validateUserInterfaceItem:
where they would actually get called.
I've tried, amongst other things, implementing these methods in the already existing App delegate but no joy. Am I perhaps misunderstanding something with the First Responder chain?
Also, logically, the other possibility is that the menu items actually do have Targets (the above assumes they don't). But, again, I can't seem to be able to identify their targets (so that I may implement validateMenuItem:
there).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在
NSPopupMenuCell
中动态(通过绑定)创建的NSMenuItem
的目标,结果是NSPopupMenuCell
本身(因此您可以将其子类化并实现validateMenuItem:
那里)。我想这是因为
NSPopupMenuCell
需要执行 KVC 调用(即setValue:forKey:
)。The Target of dynamically (via bindings) created
NSMenuItem
in aNSPopupMenuCell
, turns out to be theNSPopupMenuCell
itself (so you can subclass it and implementvalidateMenuItem:
there).I would imagine it's because
NSPopupMenuCell
would need to perform KVC calls (i.e.setValue:forKey:
).如果您使用绑定来准备内容,您还可以将每个
NSMenuItem
的enabled
属性绑定到模型中的一个布尔值,表示是否应启用它。If you're using bindings to prepare the contents, you can also bind each
NSMenuItem
'senabled
property to a boolean in the model expressing whether it should be enabled.