0

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.

Kahsn
  • 1,045
  • 3
  • 15
  • 25
  • I think http://stackoverflow.com/questions/6872852/popping-and-pushing-view-controllers-in-same-action will answer your question; there are a couple of approaches to solve the pop-then-push problem. – Joshua Kaden Feb 08 '16 at 20:47

1 Answers1

0

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.

Alex Blair
  • 584
  • 1
  • 4
  • 19