如何使嵌入式视图控制器成为响应者链的一部分? [英] How to make embedded view controller part of the responder chain?

查看:79
本文介绍了如何使嵌入式视图控制器成为响应者链的一部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用情节提要开发Mac应用程序.我有一个显示NSViewController作为其内容的窗口,其中包含一个嵌入NSSplitViewController的容器视图控制器".

I am developing a Mac app using storyboards. I have a window that presents an NSViewController as its contents, which contains a "container view controller" that embeds an NSSplitViewController.

NSSplitViewController的预期行为是使其成为响应者链的一部分,以便在第一个响应者上触发toggleSidebar动作的菜单项实际上会使标记为侧边栏的NSSplitViewController项折叠

The expected behaviour is for the NSSplitViewController to be part of the responder chain, such that a menu item that triggers the toggleSidebar action on the first responder actually collapses the item of the NSSplitViewController that's marked as a sidebar.

但是,这根本不会发生,并且菜单项保持禁用状态.所以我的问题是,如何让NSSplitViewController成为响应者链的一部分?

However, this simply does not happen and the menu item remains disabled. So my question is, how can-I get the NSSplitViewController to be part of the responder chain?

推荐答案

我注意到其中一些解决方案已经奏效,但是我从

I noticed that maybe some of these solutions have worked, but i adapted a more general purpose answer from https://stackoverflow.com/a/30938725/6938357.

我在NSViewController上进行了扩展,以寻找补充目标.可在NSSplitViewController以及带有子级的任何常规NSViewController上使用.

I made an extension on NSViewController to look for supplemental targets. Works on NSSplitViewController as well as any general NSViewController with child(ren).

extension NSViewController {

    open override func supplementalTarget(forAction action: Selector, sender: Any?) -> Any? {
        if let target = super.supplementalTarget(forAction: action, sender: sender) {
            return target
        }

        for child in children {
            var target = NSApp.target(forAction: action, to: child, from: sender) as? NSResponder

            if target?.responds(to: action) == false {
                target = child.supplementalTarget(forAction: action, sender: sender) as? NSResponder
            }

            if target?.responds(to: action) == true {
                return target
            }
        }

        return nil
    }
}

如果只希望它在单个视图控制器上进行搜索,请将该实现放在那里.此扩展适用于 all NSViewController s及其子类.

If you only want this to search on a single view controller, put this implementation there instead. This extension applies to all NSViewControllers and its subclasses.

这篇关于如何使嵌入式视图控制器成为响应者链的一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆