我有一个在 iPhone os 2.2.1 上完美运行的应用程序,但当我尝试在 iPhone os 3.0 上运行它时,它崩溃了。
这是我从控制台收到的错误:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Changing the delegate of a tab bar managed by a tab bar controller is not allowed.'
可能会发生这种情况,因为我正在以编程方式更改某个 View Controller 的 View 。
这是代码:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear: animated];
self.view = current_controller.view;
[current_controller viewWillAppear: NO];
[current_controller viewDidAppear: NO];
}
这部分代码可能出现错误,如果是,我该如何修复它? 否则为什么会发生这种情况?
预先感谢您, 伊利亚。
最佳答案
在大多数情况下,您可以使用 UITabBarControllerDelegate 代替。它具有与 UITabBarDelegate 类似的方法并避免此类异常。例如,代替:
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
int index = [tabBar determinePositionInTabBar:item]; // custom method
[tabBar doSomethingWithTabBar];
[item doSomethingWithItem];
[item doSomethingWithItemAndIndex:index];
}
你可以写:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
UITabBarItem *item = [tabBarController.tabBar selectedItem];
int index = [tabBarController.tabBar determinePositionInTabBar:item]; // custom method
[tabBarController.tabBar doSomethingWithTabBar];
[item doSomethingWithItem];
[item doSomethingWithItemAndIndex:index];
}
关于iphone - "Changing the delegate of a tab bar"异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/907857/