0

I have an NSMenuItem that is enabled/disabled based on whether or not the first responder responds to a specific selector.

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

I have an NSMenuItem called "Capture Frame", which turns a frame of a video into an image. But obviously this should be disabled on an ImageDocument, and only enabled on a VideoDocument.

The NSMenuItem is enabled if the first responder responds to -captureFrame

The VideoDocumentController has a method, -captureFrame, so it's enabled. The ImageDocumentController does not, so the menu item is never enabled.

But, there are specific VideoDocuments that I do not want to have that menu item enabled for. What could be the best way for me to disable this menu item in order to handle special cases?

A O
  • 5,516
  • 3
  • 33
  • 68

1 Answers1

2

In your VideoDocument class, override the NSDocument method validateUserInterfaceItem:. Alternatively, you can override validateUserInterfaceItem: in a subclass of NSDocumentController. Be sure to call super to keep the built-in functionality of this method. See Apple's docs on UI validation for an example.

JWWalker
  • 22,385
  • 6
  • 55
  • 76
  • Ah okay, thanks! Your answer is actually a little off, I had to use `-validateMenuItem` on the controller of the document, and I also got an exception when I called `super`, so I didn't do that. And then everything worked :D Could you maybe edit your answer before I mark correct? – A O Nov 03 '15 at 21:58
  • I added the possibility of overriding in NSDocumentController. But if it didn't work, you did something wrong. The docs on NSDocumentController even say you should call `[super validateUserInterfaceItem: ]` in the override. – JWWalker Nov 03 '15 at 22:39
  • Ah I see the issue, I said `VideoDocumentController`, not realizing that it implied I had a subclass of `NSDocumentController`. My "controller" in this case is a subclass of `NSViewController` that displays the `VideoDocument` – A O Nov 03 '15 at 22:56