Tab Bar Controller


 

Tab Bar Controller

In this application we are going to create it by using Window based application.

In this application we are going to create it by using Window based application.

Tab Bar Controller

In this Tutorial, you will learn to

      a)  Create a Tab Bar Controller

      b)  Adding view and

      c)  Adding the Tab Bar Controller to the window.

In this application we are going to create it by using Window based application.

So we will find one window.

There is  .h and  .m file.

We are going to add tab bar to Window.    

Creating a Tab Bar Controller:

Create a new project by selecting Window-Based Application.Open the Interface Builder by double clicking the MainWindow.xib file and add a Tab Bar Controller from the library. It consist of two Bar Items.

A Tab Bar Items acts as a button by which we can change to another view by clicking the Tab Bar Items.We can change the title and the image by double clicking on the Tab Bar Item Button.

Adding View:

Drag and drop the View from library to the place holder where it says View (above Tab Bar Items). We can add Any no of view(as our requirement) by Clicking View and the Tab Bar Items (we can add number of buttons on Tab Bar Controller this can be done by clicking on Tab Bar Controller and then select in Tools -> attribute Inspector and then in view Controller add as many view required by clicking the + button).

Adding Tab Bar Controller to the Window:

Now we have to add Tab Bar Controller to Window. To do this, we need to create a connection, open Interface builder and in it right click on Tab Bar Controller App Delegate and then connect Tab Bar Controller to the Variable (tabBarController) . Before connecting we have to create an instance variable of type UITabBarController and mark it with IBOutlet  in .h file so it shows up in the Interface Builder.

.h file will look like:

#import <UIKit/UIKit.h>

@interface TabBarAppDelegate : NSObject <UIApplicationDelegate>

{

    UIWindow *window;

    IBOutlet UITabBarController *tabBarController;

}

@property (nonatomic, retain) IBOutlet UIWindow *window;

@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;

@end

In this we are Declaring Tab Bar Controller as IBOutlet and also the property of it.

Make connection in interface builder as below

0

 

.m file will look like this:

1

#import "TabBarAppDelegate.h"

@implementation TabBarAppDelegate

@synthesize window, tabBarController;

2

- (void)applicationDidFinishLaunching:(UIApplication *)application

 {   

    // Override point for customization after application launch

3

    [window makeKeyAndVisible];

    [window addSubview:[tabBarController view]];

}

4

 - (void)dealloc

 {

    [window release];

5

    [tabBarController release];

    [super dealloc];

}

6

@end

In this we are synthesizing tabBarController, and also adding it to window by using [window addSubview:[tabBarController view]] and finaly releasing it.

Press  Build And Go Button.  

7

Download code from here.

 

Ads