Search code examples
swiftxcodecocoansstoryboard

How to make two windows pop out in the beginning on a Swift 4 Cocoa application?


I'm working on a Swift 4 Cocoa Mac OS X (not iOS) project with XCode 9. I have two NSWindows in my main storyboard with different (subclasses of) NSViews. Currently only one window holds the storyboard entry point and so the other one does not appear when the application begins, and I want both windows to appear when the application is loaded.

I've tried googiling with different keywords but so far couldn't find a way. The only method I found was to connect a segue from a button or menu in one window to the other window, so that the other window appear whenever the button is pressed. Is there any of making both windows appear in the beginning the 'right way' (preferablly using functionalities of XCode storyboard)?


Solution

  • You have to get a reference to the window controller in your storyboard from the second window you want to show. Add this code to your NSApplicationDelegate in applicationDidFinishLaunching. Don't forget to set the identifier from the WindowController in the Storyboard

    let storyboard = NSStoryboard(name: "Main", bundle: nil)
    let windowController = storyboard.instantiateController(withIdentifier: "MyWindowController") as! NSWindowController
    windowController.showWindow(self)