IOS5中的UITabbarcontroller抛出UIViewControllerHierarchyInconsistency异常

use*_*219 15 objective-c uitabbarcontroller ios5

我有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;
Run Code Online (Sandbox Code Playgroud)

此代码在IOS4上运行良好.我在IOS5测试版上尝试了它,并在点击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>'
Run Code Online (Sandbox Code Playgroud)

小智 23

更换:

self.view = tabBarController.view;
Run Code Online (Sandbox Code Playgroud)

有:

[self.view addSubview:tabBarController.view];
Run Code Online (Sandbox Code Playgroud)

这也将与IOS3和4向后兼容.


小智 6

在我的代码中有相同的模式(和问题).Joe的解决方案对我不起作用.看一下片段,我猜你从UIViewController派生了一个类,允许你自定义一些东西.

这里要做的事情很简单,就是从UITabBarController而不是UIViewController派生,不要创建tabBarController,并且在任何你引用tabBarController的地方,替换self.

5分钟,你不再抛出不一致的例外,你仍然向后兼容iOS 4.您仍然可以在派生类中进行所有自定义(使用导航控制器进行编辑等).

如果你已经构建了一个你需要使用的UIViewController的复杂派生,那么这可能会更多.

一个小问题 - 如果你重写LoadView,你会发现它在UITabBarController的init期间被调用.在LoadView之前设置成员很困难,因此您可能需要拆分初始化.

祝好运!


Mug*_*nth 0

您无法推送或呈现 UITabbarViewController。您的第一个视图控制器是 UITabBarController 吗?