Keep both NSSplitViewController's child controllers in first responder chain

前端 未结 2 1880
挽巷
挽巷 2021-01-03 02:50

I\'ve a document based app with an NSSplitViewController as the main window\'s content view controller. The left pane contains a custom view with controller, which implement

相关标签:
2条回答
  • 2021-01-03 03:18

    In Swift 4 you can do the following:

    override func supplementalTarget(forAction action: Selector, sender: Any?) -> Any? {
        for childViewController in childViewControllers {
            if childViewController.responds(to: action) {
    
                return childViewController
            } else {
                guard let supplementalTarget = childViewController.supplementalTarget(forAction: action, sender: sender) else {
                    continue
                }
    
                return supplementalTarget
            }
        }
    
        return super.supplementalTarget(forAction: action, sender: sender)
    }
    
    0 讨论(0)
  • 2021-01-03 03:25

    Connect to First Responder.

    You can have all child view controllers respond to actions by implementing -[NSResponder supplementalTargetForAction:sender:] in your NSSplitViewController subclass:

    - (id)supplementalTargetForAction:(SEL)action sender:(id)sender
    {
        id target = [super supplementalTargetForAction:action sender:sender];
    
        if (target != nil) {
            return target;
        }
    
        for (NSViewController *childViewController in self.childViewControllers) {
            target = [NSApp targetForAction:action to:childViewController from:sender];
    
            if (![target respondsToSelector:action]) {
                target = [target supplementalTargetForAction:action sender:sender];
            }
    
            if ([target respondsToSelector:action]) {
                return target;
            }
        }
    
        return nil;
    }
    
    0 讨论(0)
提交回复
热议问题