articles

Home / DeveloperSection / Articles / iOS : Creating Sample on Tab Bar controller

iOS : Creating Sample on Tab Bar controller

Tarun Kumar3278 21-Aug-2015

Here we are going to create a sample using 'UITabBarController' class. It is a specialized view controller much like the tabs in your browser where each tab represents a specific view basically the site you are visiting. Here the tabs are created at the bottom of the window and each tab is associated with a specific view controller. Each tabs view can have its own look and feel as they may represent different functionality in your application. You can even add a navigation controller as one of the tab's view controller if needed. Tabbar items can be customized using image icons and specific titles. 

Necessary steps for our sample application:

 

Step 1. First, create a single view application and named it TabBar.

 

Step 2. Associate two view controllers to the tab bar controller

 

Step 3. Customize the look and feel of the tabs such as background color, text fonts, etc.

 

Step 4. Set the title and image icons for the tab bar items

 

Step 5. After single view application created, then select File -> New -> File...

 

Step 6. Then, in iOS Cocoa Touch section select objective-C class and click Next.

 

Step 7. Give a name to the Class “Tab1ViewController” and select Subclass “UIViewController”.

 

Step 8. and then select create, similarly Step 7 create another objective-C class and named it

“Tab2ViewController”.

 

Step 9. Now, in MainStoryboard create two ViewControllers.

 

Step 10. except mainstoryboard, select first ViewController and go to the Identity Inspector and select class Tab1ViewController, same as select second ViewController and in Identity Inspector select class Tab2ViewController.


Here is the Source Code of our program:
Tab1ViewController.h


#import <UIKit/UIKit.h>

@interface Tab1ViewController : UIViewController< UITableViewDataSource, UITableViewDelegate>

 

@end

 

Tab1ViewController.m


#import "Tab1ViewController.h"

@interface Tab1ViewController ()

@end

 

@implementation Tab1ViewController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

}

 

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end


Tab2ViewController.h


#import <UIKit/UIKit.h>

@interface Tab2ViewController : UIViewController

@end


Tab2ViewController.m


#import "Tab2ViewController.h"

@interface Tab2ViewController ()

@end

@implementation Tab2ViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

    }

    return self;

}

 

- (void)viewDidLoad {

   [super viewDidLoad];

   / Do any additional setup after loading the view.

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end


Now, when we Run the project, our project look like this: at the bottom of the app we can toggle between the two tabs and both have different views:

iOS : Creating Sample on Tab Bar controller


Updated 07-Sep-2019

Leave Comment

Comments

Liked By