gpt4 book ai didi

ios - 在 2 个屏幕导航 Controller 之间交换 View Controller 会导致不一致的层次结构异常

转载 作者:可可西里 更新时间:2023-11-01 06:16:54 29 4
gpt4 key购买 nike

我有一个容器 View Controller ,它并排处理 2 个导航 Controller 。有时我需要将一堆 View Controller 从一个导航堆栈移动到另一个导航堆栈的底部。

问题是我遇到了 iOS 5 中新增的 UIViewControllerHierarchyInconsistency 异常。我不确定如何解决这个问题。使用 push/pop 方法效果很好,但是我需要在堆栈的下方插入一些东西,这样我才能访问 View Controller 数组本身。代码是:

- (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];

}

}

完整的异常(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>'

最佳答案

我有一个 response from the developer forums :

When you grab the UINavigationController's view controllers and insert them into the other nav controller, they are still logically in the first navigation controller, hence the exception.

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

所以我将处理原始的导航 Controller 并为每个 VC 转换重新创建它们。

关于ios - 在 2 个屏幕导航 Controller 之间交换 View Controller 会导致不一致的层次结构异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8260086/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com