Open New Window in Swift

空扰寡人 提交于 2019-12-04 23:33:11

问题


I am trying to open a new window in my Swift application but I cannot get it to open.

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.

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.


回答1:


After the openMyWindow() method is executed, the windowController will be released and consequently the window is nil. 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?


来源:https://stackoverflow.com/questions/30921737/open-new-window-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!