首页 > 解决方案 > Close/Dismiss NSViewController, macOS

问题描述

I have a NSViewController connected as such:

enter image description here

In the bottom viewController, I try to dismiss it using self.dismiss(self) however, it produces this error:

[General] dismissViewController:: Error: maybe this view controller was not presented?

How can I ever dismiss this viewcontroller?

Any help is appreciated, thanks.

标签: swiftmacos

解决方案


所以这就是我所做的:

在显示的窗口(NSViewController)中添加以下内容:将其作为属性添加到显示的类的顶部:

class FooViewController: NSViewController {  
// reference to a window  
var window: NSWindow?  
...  
}  

添加此 viewDidAppear 覆盖,而不是 viewDidLoad,因为窗口句柄为零。

override func viewDidAppear() {  
    // After a window is displayed, get the handle to the new window.  
    window = self.view.window!  
} 
  1. 现在,您当前拥有“dismissViewController”,您可以使用:

    window?.performClose(nil) // nil 因为我没有返回消息


推荐阅读