在Swift中打开新窗口 [英] Open New Window in Swift

查看:530
本文介绍了在Swift中打开新窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Swift应用程序中打开一个新窗口,但无法打开它.

I am trying to open a new window in my Swift application but I cannot get it to open.

class AppDelegate: NSObject, NSApplicationDelegate 
{
    func applicationDidFinishLaunching(aNotification: NSNotification)
    {
        openMyWindow()
    }

    func openMyWindow()
    {
        if let storyboard = NSStoryboard(name: "Main",bundle: nil)
        {
            if let vc = storyboard.instantiateControllerWithIdentifier("MyList") as? MyListViewController
            {
                var myWindow = NSWindow(contentViewController: vc)
                myWindow.makeKeyAndOrderFront(self)
                let controller = NSWindowController(window: myWindow)

                controller.showWindow(self)

            }
        }
    }
}

我已经在IB中设置了Storyboard ID.

I have set the Storyboard ID in IB.

我已经跟踪了代码,它确实进入了打开窗口的代码,但是它什么也没做.

I have traced the code and it does get into the window opening code but it doesn't do anything.

顺便说一句,我确实设置了默认的情节提要板入口点,并且该默认窗口打开了OK,但是我需要打开第二个窗口,这是行不通的.

BTW I do have a default storyboard entry point set and that default window opens OK but I need to have a second window open and that is what is not working.

推荐答案

在执行openMyWindow()方法后,将释放windowController,因此窗口为nil.这就是为什么它不存在的原因.

After the openMyWindow() method is executed, the windowController will be released and consequently the window is nil. That's why it is not there.

您必须在班级中按住该窗口以使其保持活动状态,然后该窗口才可见.

You have to hold the window in you class to keep it alive, then the window will be visible.

var windowController : NSWindowController?

这篇关于在Swift中打开新窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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