27

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 program is first loaded. So basically, there will be two windows showing up.

I have put a window controller on Main.storyboard where the first window also resides. However, I couldn't find the way to show this second window when the program starts. Could you please help?

Thank you.

Hakan
  • 756
  • 1
  • 8
  • 17

4 Answers4

41

In your Storyboard, select your second Window Controller. In the identity inspector, specify a name for this window controller, e.g secondWindowController

Then, in your app delegate, set up a property for the window controller:

@property NSWindowController *myController;

In your applicationDidFinishLaunching: method implementation, create a reference to the Storyboard. This way you get access your window controller from the storyboard. After that, the only thing left to do is to display the window by sending your window controller the showWindow: method.

#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate
@synthesize myController;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
NSStoryboard *storyBoard = [NSStoryboard storyboardWithName:@"Main" bundle:nil]; // get a reference to the storyboard
myController = [storyBoard instantiateControllerWithIdentifier:@"secondWindowController"]; // instantiate your window controller
[myController showWindow:self]; // show the window
}

@end
suttie
  • 705
  • 1
  • 8
  • 9
  • 1
    Thanks! I also had to set the "Storyboard ID" to "Main" and it worked. – Hakan Dec 03 '14 at 18:42
  • ```[storyBoard instantiateInitialController]``` can be used to bypass the ID issue (so long as you have set the window you want, as the initial view controller in the storyboard file. – Supertecnoboff Jun 27 '17 at 21:27
6

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.

Sam
  • 5,375
  • 2
  • 45
  • 54
5

Swift 5:

The project setup in XCode 13 has entirely changed. There is no longer an example of how to connect to the storyboard from the AppDelegate. Instead, they are hardcoding a NSWindow. I still find Storyboards useful, hence the below should come in handy. Remember to name your WindowController in Storyboard as mainWindowController.

let mainStoryboard = NSStoryboard.init(name: NSStoryboard.Name("Main"), bundle: nil)
var monitorcontroler = mainStoryboard.instantiateController(withIdentifier: NSStoryboard.SceneIdentifier("mainWindowController")) as! NSWindowController
monitorcontroler.showWindow(self)
Houman
  • 64,245
  • 87
  • 278
  • 460
2

swift 4 version :

var monitorcontroler: NSWindowController!

override init() {
    super.init()

    let mainStoryboard = NSStoryboard.init(name: NSStoryboard.Name(rawValue: "Main"), bundle: nil)
    monitorcontroler = mainStoryboard.instantiateController(withIdentifier: NSStoryboard.SceneIdentifier(rawValue: "moniteur")) as! NSWindowController
    monitorcontroler.showWindow(self)
}