当我尝试呈现视图时收到此警告 "attempt to present ViewController whose view is not in the window hierarchy"

I got this warning "attempt to present ViewController whose view is not in the window hierarchy" when I try to present a view

我的视图层次结构如下图所示:

我的问题是我想在单击 VC3 上的任何按钮时显示 VC1。这是我的代码

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("registerNavigation")
self.presentViewController(vc, animated: false, completion: nil)

但我收到此警告 "Warning: attempt to present ViewController whose view is not in the window hierarchy" 但没有任何反应。请告诉我,我做错了什么?

您正在向 navigationController 呈现,因此请使用如下代码。

let VC1 = self.storyboard!.instantiateViewControllerWithIdentifier("registerNavigation") as! ViewController
let navController = UINavigationController(rootViewController: VC1)
self.presentViewController(navController, animated:true, completion: nil)

如果您想从 VC3 显示 VC1,则不需要再次显示它,因为它已经加载到导航堆栈中。您只需要 dismiss or pop 导航堆栈中的 VC3 和 VC2。如果你展示了它然后关闭,如果你推了它然后弹出它。

您的警告的含义:您正在尝试呈现导航控制器 1 的视图层次结构中但不在导航控制器 2 的视图层次结构中的内容。!!

希望这会有所帮助:)