ios - 呈现 View Controller 时出现 View 层次结构错误

标签 ios swift uiviewcontroller

我有以下类(class):

class HomeViewController: UIViewController {
override func viewdidload() {
   super.viewdidload()
   callOtherVC()
}
   func callOtherVC() {
  let viewController = StepsViewController()
  let rootViewController = UINavigationController(rootViewController: viewController)
  self.presentViewController(rootViewController, animated: true, completion: nil)

}
}

StepsViewController 只是另一个 View Controller 。在 StepsViewController 中,我尝试关闭当前的 StepsViewController 并呈现其他 View Controller 。以下是代码。

class StepsViewController: UIViewController {
  override func viewdidload() {
   super.viewdidload()
   callSecondOtherVC()
}
 func callSecondOtherVC() {
    let vc = ViewController()
    self.addChildViewController(vc)
    self.parentViewController!.dismissViewControllerAnimated(true, completion: nil)
    vc.callOtherVC()
}
}

我初始化了ViewController(),因为我需要从ViewController调用相同的函数callOtherVC。基本上 ViewController 中的模型发生了变化,但我本质上是从 callOtherVC 函数调用相同的 UINavigationController。

每当我这样做时,我都会收到如下错误:\

Warning: Attempt to present (UINavigationController: 0x7d991600) on (HomeViewController: 0x7a6e00a0) whose view is not in the window hierarchy!

UINavigationController 来自 callSecondOtherVC ,而 HomeViewController 则保持原样。

我应该如何订购 VC?如果有人可以更多地解释 View 层次结构,我将不胜感激。

最佳答案

我认为你需要在这里做的是从 viewDidAppear 调用你的方法,而不是 viewDidLoad 。原因是 viewDidLoad 时该 View 不在 View 层次结构中。

关于ios - 呈现 View Controller 时出现 View 层次结构错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35263960/

相关文章:

ios - 什么是 iOS 5 SDK 中的容器 View ?

objective-c - 用动画交换 rootViewController

objective-c - NSSet 的迭代顺序是什么?

快速 Firebase 存储 NSInternalInconsistencyException

swift - 在 Swift 中计算 UITextInput 文本值长度的最佳方法?

IOS Segue 在自定义选项卡栏中不起作用

iOS - 垂直挤压图像

ios - 在显示 View 之前未加载来自 NIB 的 socket

ios - 在 View Controller 中访问 UIView 子类

xcode - swift 中运算符后出现意外的表达式