How to add NSViewController to a responder chain?

后端 未结 4 1983
谎友^
谎友^ 2020-12-05 15:27

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

4条回答
  •  时光说笑
    2020-12-05 16:19

    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.

提交回复
热议问题