【问题标题】:Attempt to present <UINavigationController> on firstController whose view is not in the window hierarchy尝试在视图不在窗口层次结构中的 firstController 上呈现 <UINavigationController>
【发布时间】:2016-01-13 08:59:10
【问题描述】:

我尝试在一个函数中打开第二个视图控制器,但出现此错误:

尝试在视图不在窗口层次结构中的 firstController 上呈现 &lt;UINavigationController&gt;

所需的所有解决方案:

要么将代码放入viewWillAppear() 或者在IBActionfunc 里面

我的程序需要从普通函数中打开第二个视图控制器

我尝试了所有这些代码:

代码1:

self.performSegueWithIdentifier("page2", sender:self)

结果:

错误:没有标识符为“page2”的segue

代码 2:

let secondViewController:page2 = page2()
self.presentViewController(secondViewController, animated: true,completion: nil)

结果:

错误:其视图不在窗口层次结构中!

代码 3:

let nextViewController = storyboard!.instantiateViewControllerWithIdentifier("next2") as! UIViewController
self.presentViewController(nextViewController, animated:true, completion:nil)

结果:

致命错误:意外发现 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)

结果:

视图不在窗口层次结构中的错误!

我需要一个解决方案。

【问题讨论】:

  • Segue 可能是您首先考虑的,但 segue 标识符不是视图的名称。您需要将您的第一个视图连接到情节提要中的导航视图并提供一个标识符才能使用此功能。
  • 是的,我在代码和情节提要中将 segue 的名称设置为 page2,我也将 segue 名称更改为另一个名称,但它不起作用
  • 在情节提要中截取你的 segue 截图应该会有所帮助。
  • 错误消息意味着您正在尝试呈现其父级未呈现的视图。在您的情况下,firstController 尚未添加到视图层次结构中,在视图控制器的视图即将添加到视图层次结构之前调用 viewWillAppear()

标签: ios iphone swift


【解决方案1】:

您将希望使用代码 1,但在您的故事板中,您需要从包含您的代码的 ViewController 到您想要显示的 ViewController 的 segue。 (Ctrl+从 ViewController“所有者”图标拖动到第二个 ViewController)

然后单击此转场,并在检查器中确保转场 (!) 获得名称“page2” - 因为此名称是用于运行转场的名称。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-17
    • 1970-01-01
    • 2014-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-17
    • 2016-01-06
    相关资源
    最近更新 更多