0

So I have a normal UINavigationController with its rootViewController set in the storyboard. Whenever I replace the navigation controller (don't ask why, but certain transitions in my app require the entire stack to be replaced with the new controller) using

var newController: UIViewController!
// newController is initiated at some point
...
if let navigationController = self.navigationController {
    navigationController.setViewControllers([newController])
}

And I can visually see the transition occur, but neither the viewWillDisappear or viewDidDisappear methods are called. When I subsequently replace the navigation controller stack they're called as expected. Is there something special about the root the prevents them from being called?

1 Answers1

0

Basically Navigation controller works as a stack of controllers where child controllers go on top of the stack.

If you want to remove a deck of view controllers from a navigation controller, you should use the pop method on their root:

navigationController?.popViewController(animated: true)

This will pop everything out of the stack and you will be able to replace the root with other controllers.

This other answer: How to detect if view controller is being popped of from the navigation controller? expands on the different options you have to detect when your controller gets popped e.g. use isMovingFromParentViewController.

Community
  • 1
  • 1
giobatta912
  • 180
  • 7