How to add NSViewController to the responder chain?

I find it difficult to understand and find information on how to get NSViewController to accept key and mouse events. I read somewhere that in order to register these events in NSViewController it must be added to the responder chain, but I can not find the answer on how to do this correctly.

Any help is appreciated!

+8
objective-c cocoa nsviewcontroller macos nsresponder
source share
3 answers

There is a good tutorial that can be found at CocoaWithLove.com .

It is calculated: you will create a subclass of NSView (for example, "EugeneView"), and then this subclass will have some additional methods in it, such as " setNextResponder " and " setViewController ". And these two methods should include your NSViewController in the responder chain.

+6
source share

Or, if in most cases your view of the controller is just a common container, insert your controller in the chain of responders between its view and its views. This can be done using these lines of code in your awakeFromNib: controller 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 subclass required.

+5
source share

Manual tuning in the NSViewController to the responder chain is no longer required with OS X 10.10 Yosemite. According to WWDC '14 , "they are automatically connected to the responder chain immediately after viewing them."

+4
source share

All Articles