Tab Bar Without StoryBoard in iPhone

Introduction

In this article I will create an Empty view application. Here we add three UIViewController Classes.

To better understand it we use the following procedure.

Step 1

Initially we add a FirstViewController Class of a UIViewController type.

To add the UIViewController Objective-C Class we use the following procedure.

new-file-in-iphone.jpg
 

Select the delegate class and right-click on it to add a new file.

objective-c-class-in-iphone.jpg

Select Objective-C Class and click on Next.

first-view-controller-class-in-iphone.jpg

Now Select UIViewController Class with Xib and click on Next. 

save-location-in-iphone.jpg

Now click on Create to import it to the project.

Step 2

Similarly we add second View Controller and third View Controller Classes.

Step 3

Now here we provide code for each class.

TabBarAppDelegate.h

//
// TabBarAppDelegate.h
// TabBar
//
// Created by Sachin Bhardwaj on 01/12/12.
// Copyright (c) 2012 Sachin Bhardwaj. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface TabBarAppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UITabBarController *tabBarController;

@end

TabBarAppDelegate.m

//
//  TabBarAppDelegate.m
//  TabBar
//
//  Created by Sachin Bhardwaj on 01/12/12.
//  Copyright (c) 2012 Sachin Bhardwaj. All rights reserved.
//

#import "TabBarAppDelegate.h"

#import "FirstViewController.h"

#import "SecondViewController.h"

#import "ThirdViewController.h"

@implementation TabBarAppDelegate
@synthesize tabBarController = _tabBarController;

- (void)dealloc
{
    [_window release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
    UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
    UIViewController *viewController3 = [[ThirdViewController alloc] initWithNibName:@"ThirdViewController" bundle:nil];
    self.tabBarController = [[UITabBarController alloc] init];
    self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, viewController3, nil];
    self.window.rootViewController = self.tabBarController;
    [self.window makeKeyAndVisible];

    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end

FirstViewController.h

//
// FirstViewController.h
// TabBar
//
// Created by Sachin Bhardwaj on 01/12/12.
// Copyright (c) 2012 Sachin Bhardwaj. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface FirstViewController : UIViewController

@end

FirstViewController.m

//
//  FirstViewController.m
//  TabBar
//
//  Created by Sachin Bhardwaj on 01/12/12.
//  Copyright (c) 2012 Sachin Bhardwaj. All rights reserved.
//

#import "FirstViewController.h"

@interface FirstViewController ()

@end

@implementation FirstViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = NSLocalizedString(@"Screen One", @"Screen One");
        self.tabBarItem.image = [UIImage imageNamed:@"first"];

    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

SecondViewController.h

//
// SecondViewController.h
// TabBar
//
// Created by Sachin Bhardwaj on 01/12/12.
// Copyright (c) 2012 Sachin Bhardwaj. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController

@end

SecondViewController.m

//
//  SecondViewController.m
//  TabBar
//
//  Created by Sachin Bhardwaj on 01/12/12.
//  Copyright (c) 2012 Sachin Bhardwaj. All rights reserved.
//

#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = NSLocalizedString(@"Screen Two", @"Screen Two");
        self.tabBarItem.image = [UIImage imageNamed:@"second"];

    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

ThirdViewController.h

//
// ThirdViewController.h
// TabBar
//
// Created by Sachin Bhardwaj on 01/12/12.
// Copyright (c) 2012 Sachin Bhardwaj. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ThirdViewController : UIViewController

@end

ThirdViewController.m

//
//  ThirdViewController.m
//  TabBar
//
//  Created by Sachin Bhardwaj on 01/12/12.
//  Copyright (c) 2012 Sachin Bhardwaj. All rights reserved.
//

#import "ThirdViewController.h"

@interface ThirdViewController ()

@end

@implementation ThirdViewController

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

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

Step 4 

Now we import images to the supporting file folder.

Step 5

Now run the application and see the output.

Step 6

Output 1 in iPhone:

output1-in-iphone.jpg

Output 2 in iPhone:

output2-in-iphone.jpg

Output 3 in iPhone:

output3-in-iphone.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all