本文介绍了IOS5中的UITabbarcontroller抛出UIViewControllerHierarchyInconsistency异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下UITabbarcontroller的代码:

I have the following code for the UITabbarcontroller:

NSMutableArray *arr = [[NSMutableArray alloc] init];
tabBarController = [[UITabBarController alloc] init];

FirstViewController *firstview = [[FirstViewController alloc] init];
[tabBarControllerViews addObject:firstview];
[firstview release];

 SecondViewController *secondview = [[SecondViewController alloc] init];
[tabBarControllerViews addObject:secondview];
[secondview release];

[tabBarController setViewControllers:arr animated:YES];
[arr release];

self.view = tabBarController.view;

此代码在IOS4上正常运行。我在IOS5测试版上尝试了它并在点击UITabbarItem时出现以下错误:

This code runs fine on IOS4. I tried it on IOS5 beta and get the following error when tapping on a UITabbarItem:

*** Terminating app due to uncaught exception 'UIViewControllerHierarchyInconsistency',
reason: 'child view controller:<FirstViewController: 0x6e03be0> should have parent view
controller:<MainViewController: 0x6816d20> but actual parent is:<UITabBarController: 0x6b0c110>'


推荐答案

替换:

self.view = tabBarController.view;

with:

[self.view addSubview:tabBarController.view];

这也将向后兼容IOS3& 4。

This will also be backwards compatible with IOS3&4.

这篇关于IOS5中的UITabbarcontroller抛出UIViewControllerHierarchyInconsistency异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 12:18