如何重新打开在 OS X 故事板中创建的关闭窗口

Fle*_*ore 5 macos xcode-storyboard swift

我的问题对这个问题很重要,但答案似乎不适用于 Swift/Storyboards。 Cocoa:用 X 关闭后以编程方式显示主窗口

基本上,我有一个或多或少带有菜单、窗口和 ViewController 的默认应用程序。如果用户在应用程序运行时关闭了窗口,我该如何重新打开它?

我在应用程序委托中创建了一个连接到“打开”菜单项的操作。在这个函数中,我想确保窗口是可见的。所以如果用户关闭了它,它应该会重新出现。但我不知道如何访问关闭的窗口。Storyboard 似乎不允许我在我的应用程序委托中为我的 Window 创建一个插座。

Flo*_*dis 6

这很容易存档,即使它不是一个优雅的解决方案。向主窗口控制器的应用程序委托添加一个新属性。在下面的例子中,我调用了控制器MainWindowController。

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
    var mainWindowController: MainWindowController? = nil
    func applicationShouldHandleReopen(sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
        mainWindowController?.window?.makeKeyAndOrderFront(self)
        return false
    }
}
Run Code Online (Sandbox Code Playgroud)

在主窗口控制器的初始化中,我在应用程序委托中注册了控制器:

class MainWindowController: NSWindowController {
    override func windowDidLoad() {
        super.windowDidLoad()
        // ...initialisation...
        // Register the controller in the app delegate
        let appDelegate = NSApp.delegate as! AppDelegate
        appDelegate.mainWindowController = self
    }
}
Run Code Online (Sandbox Code Playgroud)

就是这样,非常适合我。