Pages

Monday, January 28, 2013

Work with UITabBarController dynamically


Here i have shown How to work with UITabBarController and How to add Items in It.
1. declare tabs in class ".h" file

    UITabBarController *tabs;

2. Then synthesize in ".m" file
@synthesize tabs;

3. Now Initiate this in viewLoaded and call createTabs

    tabs = [[UITabBarController alloc] init];
    [self createTabs];



- (void) createTabs
{
    UIViewController *viewController1 = [[FirstTabViewController alloc] initWithNibName:@"FirstTabViewController" bundle:nil];
    UIViewController *viewController2 = [[SecondTabViewController alloc] initWithNibName:@"SecondTabViewController" bundle:nil];
    UIViewController *viewController3 = [[ThirdTabViewController alloc] initWithNibName:@"ThirdTabViewController" bundle:nil];
    UIViewController *viewController4 = [[FourthTabViewController alloc] initWithNibName:@"FourthTabViewController" bundle:nil];
   
    tabs.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, viewController3, viewController4, nil];
    
    UITabBarItem *item1 = [[UITabBarItem alloc] initWithTitle:@"First" image:[UIImage imageNamed:@"1.png"] tag:1];
    UITabBarItem *item2 = [[UITabBarItem alloc] initWithTitle:@"Second" image:[UIImage imageNamed:@"2.png"] tag:2];
    UITabBarItem *item3 = [[UITabBarItem alloc] initWithTitle:@"Third" image:[UIImage imageNamed:@"3.png"] tag:3];
    UITabBarItem *item4 = [[UITabBarItem alloc] initWithTitle:@"Logout" image:[UIImage imageNamed:@"4.png"] tag:4];
    
    [viewController1 setTabBarItem:item1];
    [viewController2 setTabBarItem:item2];
    [viewController3 setTabBarItem:item3];
    [viewController4 setTabBarItem:item4];
    [self.view addSubview:tabs.view];
}