简体   繁体   English

我可以有条件地禁用仅在第一个响应者响应选择器时启用的NSMenuItem吗?

[英]Can I conditionally disable an NSMenuItem that is only enabled when the first responder responds to a selector?

I have an NSMenuItem that is enabled/disabled based on whether or not the first responder responds to a specific selector. 我有一个NSMenuItem ,它根据第一响应者是否响应特定的选择器来启用/禁用。

My problem is that there are conditions in which I do not want the item enabled for the first responder. 我的问题是在某些情况下,我希望为第一响应者启用该项目。

But I can't just conditionally remove a method definition during runtime to disable the menu item 但是我不能只在运行时有条件地删除方法定义以禁用菜单项

For an example of what I mean: 对于我的意思的示例:
I have a NSDocument -based application, and I have two main subclasses-- an ImageDocument and a VideoDocument 我有一个基于NSDocument的应用程序,并且有两个主要的子类ImageDocumentVideoDocument

I have an NSMenuItem called "Capture Frame", which turns a frame of a video into an image. 我有一个名为“ Capture Frame”的NSMenuItem ,它可以将视频的帧转换为图像。 But obviously this should be disabled on an ImageDocument , and only enabled on a VideoDocument . 但是显然,这应该在ImageDocument上禁用,而只能在VideoDocument上启用。

The NSMenuItem is enabled if the first responder responds to -captureFrame NSMenuItem如果第一个响应者响应启用-captureFrame

The VideoDocumentController has a method, -captureFrame , so it's enabled. VideoDocumentController有一个方法, -captureFrame ,所以它的启用。 The ImageDocumentController does not, so the menu item is never enabled. ImageDocumentController没有,因此菜单项永远不会启用。

But , there are specific VideoDocument s that I do not want to have that menu item enabled for. 但是 ,有些特定的VideoDocument我不想启用该菜单项。 What could be the best way for me to disable this menu item in order to handle special cases? 对我来说,禁用此菜单项以处理特殊情况的最佳方法是什么?

In your VideoDocument class, override the NSDocument method validateUserInterfaceItem: . 在您的VideoDocument类中,重写NSDocument方法validateUserInterfaceItem: VideoDocument Alternatively, you can override validateUserInterfaceItem: in a subclass of NSDocumentController . 另外,您可以在NSDocumentController的子类中重写validateUserInterfaceItem: NSDocumentController Be sure to call super to keep the built-in functionality of this method. 确保调用super以保留此方法的内置功能。 See Apple's docs on UI validation for an example. 有关示例,请参阅Apple的UI验证文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM