Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add NSViewController to a responder chain?

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!

like image 728
Eugene Gordin Avatar asked Nov 19 '13 00:11

Eugene Gordin


2 Answers

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.

like image 73
Guy Moreillon Avatar answered Oct 23 '22 16:10

Guy Moreillon


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.

like image 32
heeleeaz Avatar answered Oct 23 '22 17:10

heeleeaz