简体   繁体   English

在Swift中打开新窗口

[英]Open New Window in Swift

I am trying to open a new window in my Swift application but I cannot get it to open. 我试图在我的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)

            }
        }
    }
}

I have set the Storyboard ID in IB. 我在IB中设置了Storyboard ID。

I have traced the code and it does get into the window opening code but it doesn't do anything. 我已经跟踪了代码,它确实进入了窗口打开代码,但它没有做任何事情。

BTW I do have a default storyboard entry point set and that default window opens OK but I need to have a second window open and that is what is not working. 顺便说一句,我确实有一个默认的故事板入口点设置,默认窗口打开确定,但我需要打开第二个窗口,这是不起作用的。

After the openMyWindow() method is executed, the windowController will be released and consequently the window is nil. 执行openMyWindow()方法后, openMyWindow()将被释放,因此窗口为零。 That's why it is not there. 这就是它不存在的原因。

You have to hold the window in you class to keep it alive, then the window will be visible. 您必须在您的类中保持窗口以使其保持活动状态,然后窗口将可见。

var windowController : NSWindowController?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM