macos - 在 Swift 中打开新窗口

标签 macos swift cocoa storyboard nswindow

我想在我的 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。

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

顺便说一句,我确实有一个默认的 Storyboard入口点设置,默认窗口打开正常,但我需要打开第二个窗口,这是不起作用的。

最佳答案

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

你必须在你的类中按住窗口以使其保持事件状态,然后窗 Eloquent 会可见。

var windowController : NSWindowController?

关于macos - 在 Swift 中打开新窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30921737/

相关文章:

python - 多次 fork 调用会导致 BlockingIOError

swift - 当我在 Swift 中使用 join() 方法时,不间断空格不起作用

ios - Swift:绘制矩形的最快方法

cocoa - NSTableViewCell 上的自定义背景图像

c++ - 在 .mm 文件中无法识别预编译 header 中的#define

c# - 使用 C# 的 MacOs 事件窗口标题

macos - 命令行中的 Codedesign .app 文件

iphone - GCD、线程、程序流程和 UI 更新

swift - 使用 AWS Cognito 自动登录

cocoa - 如何使用CGWindowListCopyWindowInfo从窗口列表中获取程序的信息