7
votes

For the navigation in my app I'm using a UITabBarController. This works fine, but in one of my viewcontrollers I want to push another view controller into the tabbar view. In other words I want to replace the selected viewcontroller with another one. I'm doing this with the following code:

self.tabBarController.selectedViewController = self.otherViewController;

The list of viewControllers in my TabBarController does not contain the otherViewController. This trick works fine in IOS 4.3, but IOS 5 does not like it.

Does anyone know a solution which is accepted by IOS 5?

2

2 Answers

12
votes

You want to REPLACE that view controller in the tabbar with another view Controller? If so, you have to edit the viewControllers property in the tabbar by setting a new one. It would be something like:

UIViewController *thisIsTheViewControllerIWantToSetNow;
int indexForViewControllerYouWantToReplace;

NSMutableArray *tabbarViewControllers = [self.tabbar.viewControllers mutableCopy];

[tabbarViewControllers replaceObjectAtIndex:indexForViewControllerYouWantToReplace withObject:thisIsTheViewControllerIWantToSetNow];

self.tabbar.viewControllers = tabbarViewControllers;

[tabbarViewControllers release];
0
votes

You can't just use a navigation controller or similar on this tab?

Anyway this should work:

NSMutableArray *controllers = [NSMutableArray arrayWithArray:rootTabBarController.viewControllers];
[controllers replaceObjectAtIndex:rootTabBarController.selectedIndex withObject: newController];
rootTabBarController.viewControllers = controllers;