user1559227
user1559227

Reputation: 1041

change a view controller of UITabBarController

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?

Upvotes: 4

Views: 10017

Answers (3)

arlomedia
arlomedia

Reputation: 9071

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.

Upvotes: 3

Wolverine
Wolverine

Reputation: 4329

In the AppDelegate.h

UIViewController *vc1;
UIViewController *vc2;
UIViewController *vc3;

in the Appdelegate.m

in the didFinishLaunchingWithOptions

NSMutableArray *listOfViewControllers = [[NSMutableArray alloc] init];

vc1 = [[UIViewController alloc] init];
vc1.title = @"A";
[listOfViewControllers addObject:vc1];

vc2 = [[UIViewController alloc] init];
vc2.title = @"B";
[listOfViewControllers addObject:vc2];

vc3 = [[UIViewController alloc] init];
vc3.title = @"C";
[listOfViewControllers addObject:vc3];

[self.tabBarController setViewControllers:listOfViewControllers
                                 animated:YES];

Upvotes: 0

IronManGill
IronManGill

Reputation: 7226

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

Upvotes: 2

Related Questions