How to hide the initial window on start with OS X storyboards

前端 未结 4 1521
旧巷少年郎
旧巷少年郎 2021-02-07 23:00

I am creating an OS X status bar application, so I want the application to start hidden.

I have created a \"storyboard\" application, and the initial wi

4条回答
  •  终归单人心
    2021-02-07 23:29

    The way to do this is just like you tried:

    let storyboard = NSStoryboard(name: "Main", bundle: nil)
    guard let mainWC = storyboard.instantiateControllerWithIdentifier("MainWindowController") as? MainWindowController else {
       fatalError("Error getting main window controller")
    }
    // optionally store the reference here
    self.mainWindowController = mainWC
    
    mainWC.window?.makeKeyAndOrderFront(nil) // or use `.showWindow(self)`
    

    The only thing you probably forgot was to uncheck "Release when closed". This would immediately release the window and prevents the storyboard loading mechanism from finding it even if you had the right identifier.

提交回复
热议问题