Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Changing the delegate of a tab bar" exception

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.

like image 506
Ilya Suzdalnitski Avatar asked May 25 '09 19:05

Ilya Suzdalnitski


2 Answers

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];
}
like image 120
Miroslav Avatar answered Nov 07 '22 20:11

Miroslav


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

like image 37
Flash Gordon Avatar answered Nov 07 '22 22:11

Flash Gordon