osx NSStoryBoard Open a New Window Controller

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

To open a new window, add the following code somewhere where you can keep a reference to the new window (I.E., the app delegate).

Swift

let storyboard:NSStoryboard = NSStoryboard(name: "Main", bundle: nil)
guard let controller:NSWindowController = storyboard.instantiateControllerWithIdentifier("myWindowController") as? NSWindowController else { return /*or handle error*/ }
controller.showWindow(self)

Objective-C

NSStoryboard *storyBoard = [NSStoryboard storyboardWithName:@"Main" bundle:nil]; // get a reference to the storyboard
myController = [storyBoard instantiateControllerWithIdentifier:@"secondWindowController"]; // instantiate your window controller
[myController showWindow:self];

Once you create your controller make sure you keep a reference it to somewhere outside of the function call. This can be done by creating a NSWindowController variable in your app delegate, and assigning your new controller to the variable.



Got any osx Question?