Kahsn February 2016

View hierarchy error while presenting a viewcontroller

I have a following 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 is just another viewcontroller. In StepsViewController, I try to dismiss current StepsViewController and present other viewcontroller. Following is code.

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()
}
}

I initialize ViewController() because I need to call same function callOtherVC from ViewController. Basically the model in ViewController changes but I'm essentially calling same UINavigationController from callOtherVC function.

Whenever I do this, I get an error like below:\

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

UINavigationController is from callSecondOtherVC and HomeViewController is as it is.

How should I order the VCs? And if someone can more explain about the view hierarchy, I would greatly appreciate.

Answers


Alex Blair February 2016

I think what you need to do here, is call your method from viewDidAppear, rather than viewDidLoad. The reason for this is that the view is not in the view hierarchy at the time of viewDidLoad.

Post Status

Asked in February 2016
Viewed 3,100 times
Voted 4
Answered 1 times

Search




Leave an answer