在Swift中打开新窗口

iph*_*aaw 6 macos cocoa storyboard nswindow swift

我试图在我的Swift应用程序中打开一个新窗口,但我无法打开它.

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)

            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我在IB中设置了Storyboard ID.

我已经跟踪了代码,它确实进入了窗口打开代码,但它没有做任何事情.

顺便说一句,我确实有一个默认的故事板入口点设置,默认窗口打开确定但我需要打开第二个窗口,这是不起作用的.

man*_*ahn 8

openMyWindow()执行该方法后,windowController将被释放,因此窗口为零.这就是它不存在的原因.

您必须在您的类中保持窗口以使其保持活动状态,然后窗口将可见.

var windowController : NSWindowController?
Run Code Online (Sandbox Code Playgroud)