Skip to content

AA 1.16. Presenting Multiple View Controllers with UITabBarController

rayastar edited this page Feb 7, 2015 · 1 revision

A tab bar is a container controller. In other words, we create instances of UITabBar Controller and add them to the window of our application. For each tab bar item, we add a navigation controller or a view controller to the tab bar, and those items will appear as tab bar items. A tab bar controller contains a tab bar of type UITabBar. We don’t create this object manually. We create the tab bar controller, and that will create the tab bar object for us. To make things simple, remember that we instantiate a tab bar controller and set the view controllers of that tab bar to instances of either UIViewController or UINavigationController if we intend to have navigation controllers for each of the tab bar items (aka, the view controllers set for the tab bar controller). Navigation controllers are of type UINavigationController that are subclasses of UIViewController. There‐ fore, a navigation controller is a view controller, but view controllers of type UIView Controller are not navigation controllers. So let’s assume we have two view controllers with class names FirstViewController and SecondViewController.

//
//  AppDelegate.m
//  1.12
//
//  Created by marat on 06/01/2015.
//  Copyright (c) 2015 marat. All rights reserved.
//

#import "AppDelegate.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //1.16
    self.window = [[UIWindow alloc] initWithFrame:
                   [[UIScreen mainScreen] bounds]];
    [self.window makeKeyAndVisible];

    FirstViewController *firstViewController = [[FirstViewController alloc]
                                                initWithNibName:nil
                                                bundle:NULL];
    SecondViewController *secondViewController = [[SecondViewController alloc]
                                                  initWithNibName:nil
                                                  bundle:NULL];

    // Override point for customization after application launch.
    UINavigationController *firstNavigationController =
    [[UINavigationController alloc]
     initWithRootViewController:firstViewController];
    UINavigationController *secondNavigationController =
    [[UINavigationController alloc]
     initWithRootViewController:secondViewController];


    UITabBarController *tabBarController = [[UITabBarController alloc] init];
    [tabBarController setViewControllers:
     //@[firstViewController, secondViewController]];
    @[firstNavigationController, secondNavigationController]];

    self.window.rootViewController = tabBarController;
    return YES;
}

For the first view controller, we choose the title First:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"First";
        self.tabBarItem.image = [UIImage imageNamed:@"FirstTab"];
    }
    return self;
}

And for the second view controller, we pick the title Second:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"Second";
        self.tabBarItem.image = [UIImage imageNamed:@"SecondTab.png"];
        // Custom initialization
    }
    return self;
}

You can see that our view controllers do not have a navigation bar. What should we do? It’s easy. Remember that a UINavigationController is actually a subclass of UIView Controller. So we can add instances of navigation controllers to a tab bar, and inside each navigation controller, we can load a view controller.

    UINavigationController *firstNavigationController =
    [[UINavigationController alloc]
     initWithRootViewController:firstViewController];
    UINavigationController *secondNavigationController =
    [[UINavigationController alloc]
     initWithRootViewController:secondViewController];


    UITabBarController *tabBarController = [[UITabBarController alloc] init];
    [tabBarController setViewControllers:
     //@[firstViewController, secondViewController]];
    @[firstNavigationController, secondNavigationController]];

    self.window.rootViewController = tabBarController;
Clone this wiki locally