Pages

Sunday, September 18, 2011

Add and Remove Tabbar item Run Time


In ios sdk, some time we need to insert/delete tab bar item run time. So In this case, we can add and remove tab bar items by following below code.


// Remove and Add Tab bar item from tabbar controller at run time:

-(IBAction) RemoveTab:(id)sender
{
               UIViewController *itemToRemove = nil;
   
        NSMutableArray *newItems = [NSMutableArray arrayWithArray:self.tabBarController.viewControllers];
        for (UIViewController *aView in newItems)
        {
                       if ([[[aView class] description] isEqualToString:@"FirstViewController"])
            {
                //store this item to remove later
                         itemToRemove = aView;
            }
        }
        if (itemToRemove)
        {
            [newItems removeObject:itemToRemove];
              
        }
}


// Add  Tab bar item in tabbar controller at run time:

-(IBAction) AddTab:(id)sender
{
              
                   NSMutableArray *newItems = [NSMutableArray arrayWithArray:self.tabBarController.viewControllers];
                       
//Add New ViewController

                   UITabBarItem *obj = [[FirstViewController alloc] init];
// or use below
               //UITabBarItem *obj = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];

                   [newItems addObject:obj];
                        obj.title = @"First";
                        [obj release];
              
                        [self.tabBarController setViewControllers:newItems];

}


This code has worked perfectly for me, but if you have any problem inform me.
Or get any new or more efficient way.


Thanks……………..

No comments:

Post a Comment