我正在开发一个 iPad 应用程序,它有一个 View Controller (称为 ContentViewController),其中有 3 个不同的 View 。
- slider View - 从底部打开,其中包含图标列表。基于选择一个图标,我必须在内容 View 中加载一个 View Controller
- 控制 View - 屏幕左侧有几个按钮和文本
- 容器 View ——它覆盖了大部分屏幕,我想根据从 slider 中选择的图标加载 View Controller
我是这样实现的
在应用程序启动时(第一次),我通常在容器 View 中加载主视图 Controller ,它有包含应用程序相关项目的 TableView 。每个 View Controller 都在导航 Controller 内,我在容器 View 中加载该导航 Controller
当我在 slider View 中选择一个图标时,我正在加载一个 View Controller 。
以下是我在名为 ContentViewController 的 View Controller 中执行此操作的代码:
- (void) itemSelected: (UIViewController *) viewController
{
// I am storing view controller in a instance variable currentViewController. The currentViewController is declared as @property (nonatomic , strong) UIViewController *currentViewController under @interface in header file
if(_currentViewController == nil)
{
// This part of code gets executed for the first time, when there is no view controller available in ContainerView
_currentViewController = viewController;
[self addChildViewController:_currentViewController];
[self.containerView addSubview:_currentViewController.view];
}
else if(_currentViewController != viewController)
{
// If a view controller is already opened in Container View and when I click a icon from the slider, this par of code is getting executed
[self addChildViewController:viewController];
[self transitionFromViewController:_currentViewController
toViewController:viewController
duration:0
options:UIViewAnimationOptionTransitionNone
animations:^{}
completion:^(BOOL finished){
[_currentViewController removeFromParentViewController];
_currentViewController = viewController;
[_currentViewController didMoveToParentViewController:self];
}
];
}
}
上面提到的代码在 iPad2 和 iPad3 上运行良好,它们都是 32 位设备。但是当我在 iPad Air(64 位设备)上运行这个应用程序时,它在 transitionFromViewController 中崩溃并抛出以下错误:
Terminating app due to uncaught exception 'UIViewControllerHierarchyInconsistency', reason: 'child view controller:<UINavigationController: 0x136d76130> should have parent view controller:(null) but actual parent is:<ContentViewController: 0x136d39680>'
*** First throw call stack:
(0x183642950 0x19001c1fc 0x183642890 0x186688f00 0x18661484c 0x186613ff4 0x10009a224 0x1001104c8 0x18673d798 0x1867fe234 0x186692470 0x1865fe4a4 0x1836030a8 0x183600330 0x1836006bc 0x1835416d0 0x1891ddc0c 0x186672fdc 0x100058028 0x19060faa0)
libc++abi.dylib: terminating with uncaught exception of type NSException
我尝试了各种选项,例如删除 transitionFromViewController 并替换为以下代码:
[_currentViewController willMoveToParentViewController:nil];
[_currentViewController removeFromParentViewController];
_currentViewController = firstView;
[_currentViewController didMoveToParentViewController:self];
[self addChildViewController:_currentViewController];
[self.containerView addSubview:_currentViewController.view];
但它在最后一行 [self.containerView addSubview....] 再次崩溃,并出现与上述 iPad Air 相同的错误。我不确定如何进行,也不知道为什么这个问题只发生在 64 位设备上。有人可以帮我解决这个问题吗。
提前致谢!
维涅什
最佳答案
我可以通过更改代码来解决此问题。在前面的代码中,我将 View Controller 添加为 subview Controller ,并从父 View 中删除了以前的 View Controller 。该代码在非 64 位设备上运行良好,但在 64 位设备上运行不佳。所以我只是将 View Controller 的 View 添加到容器 View 的 subview 中,并从 super View 中删除了以前的 View Controller 。下面是修改后的代码供引用:
if(_currentViewController == nil)
{
_currentViewController = viewController;
[self.containerView addSubview:viewController.view];
}
else if(_currentViewController != viewController)
{
[_currentViewController.view removeFromSuperview];
[self.containerView addSubview:viewController.view];
_currentViewController = viewController;
}
它在所有设备上都能正常工作。
关于ios - 64 位 iOS 设备中的 UIViewControllerHierarchyInconsistency,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22744059/