可可窗口没有显示在macOS Sierra上

问题描述 投票:1回答:2

我有以下代码在我的macOS应用程序中显示一个新窗口。这是在completionHandlerNSOpenPanel内。

let mainStoryBoard = NSStoryboard(name: NSStoryboard.Name(rawValue: "Main"), bundle: nil)
let windowController = mainStoryBoard.instantiateController(withIdentifier: NSStoryboard.SceneIdentifier(rawValue: "WindowController")) as! NSWindowController
let myController = windowController.window!.contentViewController as! ViewController
myController.imageView.image = mainImage
windowController.showWindow(self)

mainImage在上面定义。

问题是这个代码在macOS High Sierra上完美运行,但在macOS Sierra上,窗口有时会显示一瞬间然后消失。

为什么这会在macOS High Sierra上运行,而不是macOS Sierra呢?

swift macos cocoa nswindow nswindowcontroller
2个回答
3
投票

我能够通过修正windowController的范围来解决这个问题。将变量声明移到完成处理程序之上并将其设置为nil以开始修复问题。

我还添加了NSWindowDelegate并使用函数windowWillClosewindowController设置回nil以确保它被正确释放。


0
投票

取消选中NSWindow的延期选项。

© www.soinside.com 2019 - 2024. All rights reserved.