我有以下代码在我的macOS应用程序中显示一个新窗口。这是在completionHandler
的NSOpenPanel
内。
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呢?
我能够通过修正windowController
的范围来解决这个问题。将变量声明移到完成处理程序之上并将其设置为nil
以开始修复问题。
我还添加了NSWindowDelegate
并使用函数windowWillClose
将windowController
设置回nil
以确保它被正确释放。
取消选中NSWindow的延期选项。