Initializing another window using storyboard for OS X

前端 未结 4 1636
春和景丽
春和景丽 2021-02-02 09:46

I have created a Cocoa application in Xcode6 which uses storyboards. As a template, Xcode provides a window for the application. I want to add a second window to show when the p

4条回答
  •  借酒劲吻你
    2021-02-02 10:17

    Swift 3 version:

    var myWindowController: NSWindowController!
    
    override init() {
        super.init()
    
        let mainStoryboard = NSStoryboard.init(name: "Main", bundle: nil)
        myWindowController = mainStoryboard.instantiateController(withIdentifier: "myWindowControllerStoryboardIdentifier") as! NSWindowController
        myWindowController.showWindow(self)
    }
    

    Make sure you define myWindowController outside the function or else the window won't show up.

    It's actually better to do this in init() (of AppDelegate) as you may need it earlier on.

提交回复
热议问题