问题
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")
vc.showWindow(nil)
}
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? Thanks!
回答1:
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.
来源:https://stackoverflow.com/questions/37953123/nswindow-automatically-closes-after-showwindow