[英]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
的应用程序,并且有两个主要的子类ImageDocument
和VideoDocument
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.