在 Swift 中打开新的 Window

Open New Window in Swift

我正在尝试在我的 Swift 应用程序中打开一个新的 window,但我无法打开它。

class AppDelegate: NSObject, NSApplicationDelegate 
{
    func applicationDidFinishLaunching(aNotification: NSNotification)
    {
        openMyWindow()
    }

    func openMyWindow()
    {
        if let storyboard = NSStoryboard(name: "Main",bundle: nil)
        {
            if let vc = storyboard.instantiateControllerWithIdentifier("MyList") as? MyListViewController
            {
                var myWindow = NSWindow(contentViewController: vc)
                myWindow.makeKeyAndOrderFront(self)
                let controller = NSWindowController(window: myWindow)

                controller.showWindow(self)

            }
        }
    }
}

我在IB中设置了Storyboard ID。

我已经追踪到代码,它确实进入了 window 打开代码,但它什么也没做。

顺便说一句,我确实有一个默认的情节提要入口点设置,默认的 window 打开正常,但我需要打开第二个 window,这是行不通的。

执行完openMyWindow()方法后,windowController会被释放,因此window为nil。这就是它不存在的原因。

你必须将 window 保持在 class 中以使其保持活力,然后 window 才会可见。

var windowController : NSWindowController?