Iphone – Adding a UITabBarController programmatically with a UINavigationBarController as the first tab TO an existing Navigation Controller

iphoneuitabbarcontroller

I currently have a UINavigationController in my app delegate where I push a ViewController on to login. If the login is successful I want to then create a UITabBarController with a Navigation Controller as the first Tab whose root controller is a UIViewController that I am creating.

The RootViewController of my first UINavigationController is actually acting as a delegate to the logincontroller so if a user logs in correctly it calls a method in my RootViewController which is where I would then like to push a UITabBarController onto the stack. Here is my code:

UITabBarController *tbController = [[UITabBarController alloc] init];
    FileBrowserViewController *fileController = [[FileBrowserViewController alloc]   init];
    fileController.pathToFileDB = pathToDBUnzipped;
    fileController.parentId = @"0";

    UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:fileController];

    NSMutableArray *aViewControllersArray = [[NSMutableArray alloc] initWithCapacity:2];
    [aViewControllersArray addObject:navController];
    [navController release];

    [tbController setViewControllers:aViewControllersArray];

    [self.navigationController pushViewController:tbController animated:YES];

    [tbController release];

Now, it is all working fine. Except 2 things. Here is the screen shot:
My iPHone

1) I can't see any uitabbar items. How do i set an image and the text for each tab?
2) I don't want that top black bar. I only want 1 bar ontop with the undo button. How do I remove the additional bar?

Best Answer

I always follow this approach when I have both a UINavigationController and a UITabbarController:
You need to start with a view based application. And then create a UITabbarController in your appDelegate file.

Appdelegate.h

UITabBarController *tabBarController;
// set properties

Appdelegate.m

// Synthesize

tabBarController = [[UITabBarController alloc] init];
tabBarController.delegate=self;

// Adding Search,Nearby,Map,AboutUs,Favorites Tabs to tabBarController  
Search * search = [[Search alloc] init];  
UINavigationController *searchNav = [[UINavigationController alloc]        initWithRootViewController:search];  

Nearby* nearby = [[Nearby alloc] init];  
UINavigationController *nearbyNav = [[UINavigationController alloc] initWithRootViewController:nearby];  

Map* map = [[Map alloc] init];  
UINavigationController *mapNav = [[UINavigationController alloc] initWithRootViewController:map];  

AboutUs* aboutUs = [[AboutUs alloc] init];  
UINavigationController *aboutUsNav = [[UINavigationController alloc] initWithRootViewController:aboutUs];  

Favorites* favorites = [[Favorites alloc] init];  
UINavigationController *favoritesNav = [[UINavigationController alloc] initWithRootViewController:favorites];  

NSArray* controllers = [NSArray arrayWithObjects:searchNav,nearbyNav,mapNav,aboutUsNav,favoritesNav, nil];  
tabBarController.viewControllers = controllers; 

[window addSubview:tabBarController.view];    

You can accordingly manage in which tab you want to place navigation controller or only a view controller.

Then in each of the view controllers mentioned above you need to implement

- (id)init {}

in which you can set Tab name and image.

I always follow this approach and it never fails. The tabs are always visible. You can make changes according to your code.

Related Topic