Switching view managers between 2 on-screen navigation controllers causes an inconsistent hierarchy exception

I have a container controller that handles 2 navigation controllers side by side. I sometimes need to move a bunch of view controllers from one navigation stack to the bottom of another navigation stack.

The problem is that I am getting a UIViewControllerHierarchyInconsistency exception, which is new in iOS 5. I am not sure how to get around this. Using push / pop methods works fine, however I need to push things further up the stack, so I refer to the view controller array itself. Code:

 - (void)swapViewController:(UIViewController *)controller { NSMutableArray *leftStack = [NSMutableArray arrayWithArray:_leftNavController.viewControllers]; NSMutableArray *rightStack = [NSMutableArray arrayWithArray:_rightNavController.viewControllers]; if ([leftStack containsObject:controller]) { // Left to right [leftStack removeObject:controller]; [rightStack addObject:controller]; [_leftNavController setViewControllers:leftStack]; [_rightNavController setViewControllers:rightStack]; } else { // Right to left [rightStack removeObject:controller]; [leftStack addObject:controller]; [_rightNavController setViewControllers:rightStack]; [_leftNavController setViewControllers:leftStack]; } } 

Full exception:

 *** Terminating app due to uncaught exception 'UIViewControllerHierarchyInconsistency', reason: 'child view controller:<MyViewController: 0x6c4e7f0> should have parent view controller:<UINavigationController: 0x6a5d100> but requested parent is:<UINavigationController: 0x6a58c10>' 
+4
source share
1 answer

I had a response from the developer forums :

When you grab the view controllers of the UINavigationController and paste them into another nav controller, they are still logically in the first navigation controller, therefore an exception.

Why not just create a new view controller for this? If necessary, you can share data or state with it, or even implement a full copy for them to make it simpler, but inconsistency is directly related to trying to have a view controller in two different controllers at the same time.

So, I will get rid of the original navigation controllers and recreate them for each VC transition.

0
source

Source: https://habr.com/ru/post/1382927/


All Articles