Reputation: 53550
I have an application that perfectly works on iPhone os 2.2.1 but when I try to run it on iPhone os 3.0 it crushes.
Here is the error I got from the console:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Changing the delegate of a tab bar managed by a tab bar controller is not allowed.'
Probably it occurs because I am changing the view of a certain view controller programmatically.
Here is the code:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear: animated];
self.view = current_controller.view;
[current_controller viewWillAppear: NO];
[current_controller viewDidAppear: NO];
}
May an error occur in this part of code and if yes how can I fix it? Why else could it occur?
Thank you in advance, Ilya.
Upvotes: 6
Views: 8770
Reputation: 548
In most cases you can use UITabBarControllerDelegate instead. It has similar methods to UITabBarDelegate and avoids such exception. For, example, instead of:
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
int index = [tabBar determinePositionInTabBar:item]; // custom method
[tabBar doSomethingWithTabBar];
[item doSomethingWithItem];
[item doSomethingWithItemAndIndex:index];
}
you can write:
- (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];
}
Upvotes: 10
Reputation: 836
I found the following solution for me:
Add the following code to your class
-(void)tabBar:(UITabBar *)tabBar willBeginCustomizingItems:(NSArray *)items{
id modalViewCtrl = [[[self view] subviews] objectAtIndex:1];
if([modalViewCtrl isKindOfClass:NSClassFromString(@"UITabBarCustomizeView")] == YES)
((UINavigationBar*)[[modalViewCtrl subviews] objectAtIndex:0]).tintColor = [UIColor blackColor];
}
Hope this helps...
Upvotes: 0
Reputation: 56
Mr. Ernst above gives the impression that he sees something in Ilya's code that constitutes "yanking a view out from under a Controller". That can have you staring at code for a long time and that isn't where the problem really is. I posted this problem on the Apple Developer Forum http://discussions.apple.com/message.jspa?messageID=10259835#10259835 and I was told that 'NSInternalInconsistencyException' is a problem with a .xib file (In the Interface Builder). Using this information I found the following solution. I think some of the name's I give here are generic and will give help to others trying to fix this problem. To review the problem, the xib reference compiles and runs perfectly on 2.x, compiles on 3.x and gives the error message above when you try to run the application in the 3.0 simulator. I had a delegate in a tab bar. In viewing the Referencing Outlets in Interface Builder I had "Multiple", "File's Owner", "Tab Bar", and "Tab Bar Controller" as the referencing outlets. When I removed "Tab Bar" from the Referencing Outlets my app ran in Simulator 3.0. It also compiled and ran on 2.x so the "Tab Bar" reference was not required by 2.x. ... Flash Gordon
Upvotes: 4
Reputation:
This is a non trivial problem. Someone must have posted an example of removing a view from under a ViewController because a lot of people did it. And despite all the talk about how "bad" it was, it worked in 2.x. Even the apple article doesn't deal with architecture issue. Most people are going to write a .h/.m combination to handle each subcontroller view. The apple example seems to operate only in the .m file that controls the tabbarcontroller.
Upvotes: 0
Reputation:
I think this is one of the tutorials that Adam Ernst is referring to:
Upvotes: 0
Reputation: 54060
Quite simply, you can't do that. Yanking the view out from under a UIViewController is a sure way to get a crash.
Look at the tab bar tutorials Apple provides to see how it's done properly.
Upvotes: 0