In a UITabBarController, upon selecting a tab, I want that tab's UIViewController to change (assign a new viewcontroller). I'm trying this-
NSMutableArray *tabBarViewControllers = [myUITabBarController.viewControllers mutableCopy];
[tabbarViewControllers replaceObjectAtIndex:0 withObject:[[myViewcontroller1 alloc] init]];
[myUITabBarController setViewControllers:tabbarViewControllers];
But it gives error. How to assign a new UIViewController and refresh instantly?
This is based on femina's answer but doesn't require you to build the whole array of view controllers, it just lets you replace an existing view controller with another. In my case I wanted to swap in a different xib file for the iPhone 5 screen:
if ([[UIScreen mainScreen] bounds].size.height == 568) {
    NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:self.tabBarController.viewControllers];
    Tracking *tracking568h = [[Tracking alloc] initWithNibName:@"Tracking-568h" bundle:nil];
    tracking568h.title = [[viewControllers objectAtIndex:0] title];
    tracking568h.tabBarItem = [[viewControllers objectAtIndex:0] tabBarItem];
    [viewControllers replaceObjectAtIndex:0 withObject:tracking568h];
    [tracking568h release];
    [self.tabBarController setViewControllers:viewControllers animated:FALSE];
}
This changes the view controller for the first tab, keeping the same tab icon and label.
Please see this code , it offers 2 tabbar's with navigation.
In the AppDelegate.h please declare
    UINavigationController *nav1;
    UINavigationController *nav2;
    UITabBarController *tab;
And in the Appdelegate.m , in the didFinishLaunchingWithOptions please add:- 
    tab = [[UITabBarController alloc]init];
    ViewController *view1 = [[ViewController alloc]init];
    nav1= [[UINavigationController alloc]initWithRootViewController:view1];    
    UITabBarItem *tab1 = [[UITabBarItem alloc]initWithTitle:@"Add" image:[UIImage imageNamed:@"Plus.png"] tag:1];
    view1.title = @"Add";
    [view1 setTabBarItem:tab1];
    SettingsViewController *view2 = [[SettingsViewController alloc]init];
    nav2= [[UINavigationController alloc]initWithRootViewController:view2];
    UITabBarItem *tab2 = [[UITabBarItem alloc]initWithTitle:@"Setting" image:[UIImage imageNamed:@"settings.png"] tag:2];
    view2.title = @"Setting";
    [view2 setTabBarItem:tab2];
    tab.viewControllers = [NSArray arrayWithObjects:nav1,nav2,nil];
    self.window.backgroundColor = [UIColor whiteColor];
    self.window.rootViewController = tab;
Also check this link for further implementation ... Hope this helps :)
UItabBar changing View Controllers
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With