【问题标题】:Open New Window in Swift在 Swift 中打开新窗口
【发布时间】:2015-09-04 11:14:27
【问题描述】:

我试图在我的 Swift 应用程序中打开一个新窗口,但我无法打开它。

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。

我已经追踪了代码,它确实进入了打开窗口的代码,但它没有做任何事情。

顺便说一句,我确实设置了默认情节提要入口点,并且该默认窗口可以正常打开,但我需要打开第二个窗口,这不起作用。

【问题讨论】:

    标签: macos swift cocoa storyboard nswindow


    【解决方案1】:

    openMyWindow()方法执行后,windowController会被释放,因此window为nil。这就是它不存在的原因。

    你必须在你的班级中保持窗口保持活动状态,然后窗口才会可见。

    var windowController : NSWindowController?
    

    【讨论】:

      猜你喜欢
      • 2014-03-24
      • 1970-01-01
      • 2021-01-05
      • 2014-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多