我尝试在一个函数中打开第二个 View Controller ,但出现此错误:
Attempt to present
<UINavigationController>
on firstController whose view is not in the window hierarchy!
所需的所有解决方案:
要么把代码放在viewWillAppear()
里面
或里面 IBAction
函数
我的程序需要从普通函数打开第二个 View Controller
我试过所有这些代码:
代码1:
self.performSegueWithIdentifier("page2", sender:self)
结果:
error: has no segue with identifier 'page2''
代码 2:
let secondViewController:page2 = page2()
self.presentViewController(secondViewController, animated: true,completion: nil)
结果:
error: whose view is not in the window hierarchy!
代码 3:
let nextViewController = storyboard!.instantiateViewControllerWithIdentifier("next2") as! UIViewController
self.presentViewController(nextViewController, animated:true, completion:nil)
结果:
fatal error: unexpectedly found nil fwom win func
代码 4:
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("next2") as! page2
self.presentViewController(nextViewController, animated:true, completion:nil)
dismissViewControllerAnimated(true, completion:nil)
结果:
error whose view is not in the window hierarchy!
我需要一个解决方案。
最佳答案
您将要使用代码 1,但是在您的 Storyboard 中,您将需要从包含您的代码的 ViewController 到您要显示的 ViewController 的转场。 (Ctrl+从 ViewController“所有者”图标拖动到第二个 ViewController)
然后单击此转场,并在检查器中确保转场 (!) 获得名称“page2”——因为此名称用于运行转场。
关于ios - 尝试在其 View 不在窗口层次结构中的 firstController 上呈现 <UINavigationController>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34762267/