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

标签 ios cocoa-touch uikit

我有一个容器 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/

相关文章:

ios - Cocos2d : how to share player data between scenes

ios - 从后台在简历上呈现 ModalViewController,避免下面的内容闪现

swift - 如何在 Scene Delegate iOS 13 中设置 rootViewController

uikit - 在 WatchKit 中访问系统颜色

ios - iOS 和 tvOS 共享的 Cocoa Touch 框架

ios - UIViewController 和 UITableViewController 有什么区别

ios - UIWebView 中的后台音频播放

iphone - 在 iPhone 上解析脏 HTML

ios - ReactiveCocoa - 忽略所有值

ios - 在 View Controller 之间传递数据