Mike Nathas Mike Nathas - 2 years ago 178
Swift Question

NSWindow automatically closes after showWindow

I'm trying to open a NSWindow inside a Storyboard.
I've instantiated the controller correctly, the window opens but disappears instantly.

var sb : NSStoryboard?
var vc : NSWindowController?
@IBAction func openWindow(sender: AnyObject) {
let sb = NSStoryboard(name: "NewStoryBoard", bundle: nil)
let vc = sb.instantiateControllerWithIdentifier("windowController")

I would understand this behavior if the vars would be inside the func. In this case ARC would kill the window.

In my sample the vars are outside the func which should keep the vars from killed by ARC.

What is wrong with my way?

Answer Source

Actually, in your sample, you have two different sets of variables. Your let sb = ... and let vc = ... create local variables (with the same name) and will override the outer variables when used in the function. You want to remove the let qualifiers so that it assigns the values to the variables on the outside of the function.

If you remove both let qualifiers, unwrap the now optionals, and force the downcast, then your window will stay up.

Note: if you do not need your storyboard again, you can actually remove the var sb... and keep the let sb... in order to have one less thing to unwrap.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download