NSWindow automatically closes after showWindow

淺唱寂寞╮ 提交于 2021-02-05 09:47:45

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!