我的问题很重要,但答案似乎不适用于 Swift/Storyboards。Cocoa:用 X 关闭后以编程方式显示主窗口 https://stackoverflow.com/questions/6201958/cocoa-programmatically-show-the-main-window-after-closing-it-with-x
基本上,我有一个或多或少的默认应用程序,带有菜单、窗口和 ViewController。如果用户在应用程序运行时关闭窗口,如何重新打开它?
我在应用程序委托中创建了一个连接到“打开”菜单项的操作。在此函数中,我想确保窗口可见。因此,如果用户关闭了它,它应该会重新出现。但我不知道如何访问关闭的窗口。故事板似乎不允许我在应用程序委托中为我的窗口创建一个出口。
这很容易归档,尽管它不是一个优雅的解决方案。向主窗口控制器的应用程序委托添加一个新属性。在下面的示例中,我将控制器称为MainWindowController
.
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
var mainWindowController: MainWindowController? = nil
func applicationShouldHandleReopen(sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
mainWindowController?.window?.makeKeyAndOrderFront(self)
return false
}
}
在主窗口控制器的初始化中,我在应用程序委托中注册控制器:
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
}
}
仅此而已,对我来说非常有效。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)