如何使嵌入式视图控制器成为响应者链的一部分?

Abs*_*Sky 7 macos cocoa first-responder nsviewcontroller swift

我正在使用故事板开发 Mac 应用程序。我有一个窗口,它显示一个NSViewController作为其内容的窗口,其中包含一个嵌入NSSplitViewController.

在此处输入图片说明

预期的行为是作为NSSplitViewController响应者链的一部分,这样触发toggleSidebar第一响应者操作的菜单项实际上会折叠NSSplitViewController标记为侧边栏的项。

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

Jam*_*nek 0

查看nextReponsder的财产NSResponder。该属性定义了响应者链。它通常会自动设置为遵循 Cocoa 框架定义的响应者更改,但您可以更改它以在不同方向插入/跳过/转移链。

例如,在某个时刻(不要问我什么时候),Cocoa 开始将窗口的控制器包含在响应者链中。为了使我的应用程序能够在所有版本的 macOS 上一致运行,我将在窗口控制器中包含如下代码:

- (void)windowDidLoad
{
    // Sent when the controller's window has been loaded from the nib
    [super windowDidLoad];
    NSWindow* window = self.window;

    // Make sure this window controller is in the responder chain
    NSResponder* nextResponder = window.nextResponder;  // get our window's next responder
    if (nextResponder!=self)
        {
        // running earlier OS X that does not include the window controller in the chain: patch us in
        self.nextResponder = nextResponder;
        window.nextResponder = self;
        }
Run Code Online (Sandbox Code Playgroud)

-windowDidLoad-viewDidLoad、 和-awakeFromNib都是调整响应者链的好地方,以便它们包含或排除您想要的任何对象。