How to add NSViewController to a responder chain?

雨燕双飞 提交于 2019-11-26 20:45:36

问题


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!


回答1:


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.




回答2:


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."




回答3:


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.



来源:https://stackoverflow.com/questions/20061052/how-to-add-nsviewcontroller-to-a-responder-chain

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!