showWindow之后,NSWindow将自动关闭 [英] NSWindow automatically closes after showWindow

查看:59
本文介绍了showWindow之后,NSWindow将自动关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在情节提要板中打开一个NSWindow.我已经正确实例化了控制器,该窗口会打开,但会立即消失.

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)
   }

如果var位于函数内部,我会理解这种行为.在这种情况下,ARC将杀死该窗口.

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

在我的示例中,变量位于函数外部,这应防止var被ARC杀死.

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!

推荐答案

实际上,在您的示例中,您有两组不同的变量.您的 let sb = ... let vc = ... 创建局部变量(具有相同的名称),并且在函数中使用时将覆盖外部变量.您想要删除let限定词,以便将值分配给函数外部的变量.

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.

如果您同时删除了两个 let 限定符,则取消包装现在的可选内容,并强制向下转换,则窗口将保持打开状态.

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

注意:如果您不再需要情节提要,则实际上可以删除 var sb ... 并保留 let sb ... 以便少一件要包装的东西.

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.

这篇关于showWindow之后,NSWindow将自动关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆