13

I'm having hard time understanding and finding info about how to make NSViewController accept key and mouse events. I read somewhere that in order to register these events in NSViewController it should be added to a responder chain, but I can't find the answer how to properly do this.

Any kind of help is highly appreciated!

Eugene Gordin
  • 4,047
  • 3
  • 47
  • 80
  • What have you done so far? What do you find when you run a search for [objective-c] [osx] nsview mouse events? – El Tomato Nov 19 '13 at 01:11
  • I have a custom view controller which is a subclass of NSViewController. This controller has a property which is a NSView. The property gets set through nib file (through IBOutlet). I know that I can subclass NSView, and implement mouse/key events there, and pass them through notification to my controller...or use setNextResponder there (as Michael said below). However, I'm curious if I can somehow ad controller to responder chain without subclassing from nsview. – Eugene Gordin Nov 19 '13 at 05:41
  • Claus Jørgensen is correct in that not all view controllers get added to the responder chain in all situations - for example if the view controller itself isn't added as a child view controller to a parent controller (but its view is somehow added to the view hierarchy through other means), that view controller will not become part of the responder chain. – coderSeb Apr 12 '18 at 16:25

4 Answers4

9

There's a nice tutorial found at CocoaWithLove.com.

Summed up: you'll create a subclass of NSView (e.g. "EugeneView") and then that subclass will have some extra methods in it, such as "setNextResponder" and "setViewController". And doing these two methods should get your NSViewController integrated into the responder chain.

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
7

Manually patching in the NSViewController into the responder chain isn't necessary anymore as of OS X 10.10 Yosemite. According to WWDC '14, "they're automatically wired up in the responder chain right after their view."

Manfred Urban
  • 428
  • 5
  • 13
  • 1
    This is incorrect, not all view controllers are automatically wired up in the responder chain. – Claus Jørgensen Apr 26 '16 at 10:33
  • 5
    @ClausJørgensen: Can you base your comment/downvote on a particular example, please? Also I would like to invite you to have a [look at the above video, 20:33](https://developer.apple.com/videos/play/wwdc2014-212/). – Manfred Urban Apr 26 '16 at 14:13
  • 1
    For OSX 10.10 and 10.11 and up I can confirm this works OK and is probably the preferred answer. – Gerrit Beuze Jun 09 '16 at 08:46
6

Or if, as is the case most of the time, your controller's view is simply a generic container, insert your controller in the responder chain between its view and its subviews. This can be done with these lines of code in your controller's awakeFromNib:

Obj-C:

[self setNextResponder:self.view];

for (NSView *subview in self.view.subviews) {
    [subview setNextResponder:self]; 
}

Swift:

override func awakeFromNib() {
    super.awakeFromNib()
    self.nextResponder = self.view
    for subview in self.view.subviews {
        subview.nextResponder = self
    }
}

No subclassing needed.

Guy Moreillon
  • 993
  • 10
  • 28
  • Using this method can cause *** CFHash() called with NULL *** crashes when targeting 10.8.5 – levarius Feb 06 '15 at 17:20
  • This is a better solution than the accepted answer because it's easier (I couldn't get the accepted answer to work on the first attempt either). Thanks. – GenericPtr Aug 09 '15 at 16:20
  • how do you do this in swift in the class ViewController: NSViewController? – Neal Davis Feb 15 '16 at 20:00
1

While debugging. I noticed the NSViewController view does not accept the first responder.

You can confirm this by printing print(viewController.view) //false

for NSViewController to be added to the responder chain, its view must acceptFirstReponder. This could easily be done by creating an extension of NSView and overriding its acceptFirstResponder

extension NSView{
    //making view acceptFirstResponder by default,
    //this will enable NSViewController receive responder event dispatched into responder chain
    open override var acceptsFirstResponder: Bool{return true}
}

With this, your controller will added to the responder chain and will receive all responder events.

My explanation may not be too accurate as I am new to Cocoa. but the solution does work perfectly well.

I did this to resolve issue of my ViewController not receiving onKeyDown event.

heeleeaz
  • 322
  • 1
  • 3
  • 13