How to add NSViewController to a responder chain?
Asked Answered
C

4

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!

Cowrie answered 19/11, 2013 at 0:50 Comment(3)
What have you done so far? What do you find when you run a search for [objective-c] [osx] nsview mouse events?Menes
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.Cowrie
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.Cheep
G
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.

Grovergroves answered 19/11, 2013 at 1:5 Comment(2)
Thank you for your response, Michael! can I somehow add viewController to the responder chain without subclassing from nsview?Cowrie
@EugeneGordin: yes, seem my answer belowCloaca
C
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."

Catholicize answered 22/2, 2016 at 16:8 Comment(3)
This is incorrect, not all view controllers are automatically wired up in the responder chain.Moretta
@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.Catholicize
For OSX 10.10 and 10.11 and up I can confirm this works OK and is probably the preferred answer.Ginni
C
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.

Cloaca answered 12/3, 2014 at 6:54 Comment(3)
Using this method can cause *** CFHash() called with NULL *** crashes when targeting 10.8.5Maddocks
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.Mufti
how do you do this in swift in the class ViewController: NSViewController?Tekla
C
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.

Croesus answered 19/3, 2020 at 15:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.