64位iOS设备中的UIViewControllerHierarchyInconsistency [英] UIViewControllerHierarchyInconsistency in 64 bit iOS device
问题描述
我正在开发一个iPad应用程序,它有一个视图控制器(称为ContentViewController),里面有3个不同的视图。
I am developing an iPad application which has a single view controller (called as ContentViewController) with 3 different views in it.
- Slider view - 从底部打开,其中包含图标列表。基于选择图标,我必须在内容视图中加载视图控制器
- 控制视图 - 屏幕左侧有几个按钮和文本
- 容器视图 - 这涵盖了大部分屏幕,我想根据滑块中的图标选择加载视图控制器
这是我实现它的方式
在应用程序启动时(第一次),我通常在Container View中加载主视图控制器,它具有包含应用程序相关项的表视图。每个视图控制器都在导航控制器内,我在容器视图中加载该导航控制器
At application start (for the first time), I normally load home view controller in Container View, which has table view with application related items. Every view controller is inside a navigation controller and I load that navigation controller in the container view
当我在滑块视图中选择一个图标时,我正在加载一个视图控制器。
When I select a icon in the slider view, I am loading a view controller.
以下是我在名为ContentViewController的视图控制器中执行此操作的代码:
The following is the code that I implemented to do this stuff in a view controller with name ContentViewController:
- (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中崩溃,引发以下错误:
The code mentioned above is working fine in iPad2 and iPad3, which are 32 bit device. But when I run this application on iPad Air (64 bit device), it is crashing in transitionFromViewController throwing following error:
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并替换为以下代码:
I tried various options like removing transitionFromViewController and replacing with following code:
[_currentViewController willMoveToParentViewController:nil];
[_currentViewController removeFromParentViewController];
_currentViewController = firstView;
[_currentViewController didMoveToParentViewController:self];
[self addChildViewController:_currentViewController];
[self.containerView addSubview:_currentViewController.view];
但它在最后一行[self.containerView addSubview ....]中再次崩溃,同时提到同样的错误在iPad Air上面。我不知道如何继续,我不知道为什么只有64位设备才会出现此问题。有人可以帮我这个。
But it again crashed in last line [self.containerView addSubview....] with same error mentioned above in iPad Air. I am not sure how to proceed and I don't why this issue happens only with 64 bit device. Could someone please help me on this.
提前致谢!
Vignesh
推荐答案
我可以通过更改代码来解决此问题。在前面的代码中,我将视图控制器添加为子视图控制器,并从父视图中删除了以前的视图控制器。该代码在非64位设备上运行良好,但在64位设备上运行不正常。所以我只是将视图控制器的视图添加到容器视图的子视图中,并从超级视图中删除了以前的视图控制器。以下是修改后的代码供参考:
I am able to resolve this issue by changing the code. In the previous code, I was adding the view controller as child view controller and removed previous view controller from parent view. That code was working fine in non-64 bit device, but not on 64 bit device. So I just added view controller's view into subview of the container view and removed previous view controller from super view. Here is the modified code for reference:
if(_currentViewController == nil)
{
_currentViewController = viewController;
[self.containerView addSubview:viewController.view];
}
else if(_currentViewController != viewController)
{
[_currentViewController.view removeFromSuperview];
[self.containerView addSubview:viewController.view];
_currentViewController = viewController;
}
它适用于所有设备。
这篇关于64位iOS设备中的UIViewControllerHierarchyInconsistency的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!