I find a bug about [UINavigationController setViewController: animated:]. and i get crash every time in iOS 9.0+. I don't try iOS 9.0-.
In my case, i have a rootViewController which has 2 childViewControllers: UINavigationControllerA, UINavigationControllerB
UINavigationControllerA has 1 childViewController, like this: UINavigationControllerA.viewControllers == [UIViewControllerA]
UINavigationControllerB also has 1 childViewController, like this: UINavigationControllerB.viewControllers == [UIViewControllerB]
then UINavigationControllerB push UIViewControllerC, so UINavigationControllerB.viewControllers == [UIViewControllerB, UIViewControllerC]
at last, put UIViewControllerC to UINavigationControllerA
[UINavigationControllerB setViewControllers:@[UIViewControllerB] animated:NO]
[UINavigationControllerA setViewControllers:@[UIViewControllerA, UIViewControllerC] animated:NO]
it will crash!
crash log:
Terminating app due to uncaught exception 'UIViewControllerHierarchyInconsistency', reason: 'child view controller:<UIViewControllerC: 0x7f7fd17453a0> should have parent view controller:<UINavigationController: 0x7f7fd2817400> but requested parent is:<UINavigationController: 0x7f7fd182f800>'
here is demo.https://github.com/l1002k/NaviSetViewControllersBugDemo
if i do [UIViewControllerC.view removeFromSuperview] before setViewControllers. it will OK, i guess the UIViewController.parentViewController reply on the UIViewController.view.superview.