如何让多个 windows 执行相同的菜单项操作?

How to get multiple windows to perform the same menu item action?

我有一个 Cocoa 应用程序支持多个 windows。

我已经知道如何让主要 window 对 NSMenu 中的任何 NSMenuItem 执行操作,包括 NSToolbar 中的 NSToolbarItem ].

class MainWindowController: NSWindowController {
    // ...
    @IBAction func doSomethingIncredible(_ sender: Any?) {
        // ...
    }
}

这对于单个 window 应用来说很容易,但我的应用支持多个 windows。

但是我怎样才能让其他 windows 除了主要的 window 来访问甚至验证来自 NSMenuNSMenuItem

Safari 就是一个例子。它支持多个windows。您可以 select 打开位置...(⌘L)、新标签 (⌘T)、显示边栏 (⇧⌘L) 等。 window。它与主 window 无关。它可以在任何 windows 上完成。我如何为我的应用程序执行此操作?

class AnotherWindowController: NSWindowController {
    // ...
    @IBAction func doSomethingIncredible(_ sender: Any?) {
        // ...
    }
}

MainWindowControllerAnotherWindowController 如何从 NSMenu 中的 NSMenuItem 之一执行相同的操作,而其中一个是重点 window?

而不是硬连接将菜单项连接到特定控制器,将其连接到 First Responder(红色立方体)。

响应者链中响应选择器的第一个对象——通常是最前面的 window——捕获并执行操作。