如何重新打开 window 应用,Xcode

How to reopen the window of app, Xcode

我正在阅读 "Cocoa programming for OS X"(The Big Neard Ranch Guid)并且我制作了一个最简单的应用程序来测试一个空的 windows 应用程序,其中包含一个 window 控制器和 xib 文件控制器。我已经根据本书编写了所有必要的代码,但是在构建应用程序并关闭应用程序的 window 后,我无法重新打开应用程序的 window。怎么了 ?在代码下方。 (我在 MainMenu.xib 中删除了 window 并相应地更改了 AppDelegate;并且未选中 Visible at Launch 框) ).书上说 showWindow(_:) 会重新打开 window,但我没有看到这个!

import Cocoa

 @NSApplicationMain
 class AppDelegate: NSObject, NSApplicationDelegate {

var control: Control?

func applicationDidFinishLaunching(aNotification: NSNotification) {

    //Create a window controller object
    let control = Control()

    //Put the window of the window controler om the screen
    control.showWindow(self)

    //Set the propertity to point to window controler
    self.control = control

}


func applicationWillTerminate(aNotification: NSNotification) {
    // Insert code here to tear down your application
}


}

在我的控制器中class

import Cocoa
class Control: NSWindowController
{
    override var windowNibName: String?
        {
            return "Control"
}
}

在文件的所有者身份中,控制控制器和 window 已连接到 window 插座。

您必须添加以下应用程序委托方法。

func applicationShouldHandleReopen(sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
    if flag {
        control.window.orderFront(self)
    } else {
        control.window.makeKeyAndOrderFront(self)
    }
    return true
}