Monday, June 10, 2013

UITabBarController viewControllers example in Objective C (iOS).

UITabBarController viewControllers

An array of the root view controllers displayed by the tab bar interface.

@property(nonatomic, copy) NSArray *viewControllers

Discussion of [UITabBarController viewControllers]
The default value of this property is nil. When configuring a tab bar controller, you can use this property to specify the content for each tab of the tab bar interface. The order of the view controllers in the array corresponds to the display order in the tab bar. Thus, the controller at index 0 corresponds to the left-most tab, the controller at index 1 the next tab to the right, and so on. If there are more view controllers than can fit in the tab bar, view controllers at the end of the array are managed by the More navigation controller, which is itself not included in this array.[UITabBarController viewControllers]

If you change the value of this property at runtime, the tab bar controller removes all of the old view controllers before installing the new ones. The tab bar items for the new view controllers are displayed immediately and are not animated into position. When changing the view controllers, the tab bar controller remembers the view controller object that was previously selected and attempts to reselect it. If the selected view controller is no longer present, it attempts to select the view controller at the same index in the array as the previous selection. If that index is invalid, it selects the view controller at index 0.

Setting this property also sets the customizableViewControllers property to the same set of view controllers.

UITabBarController viewControllers example.
for (UIViewController *v in self.tabBar.viewControllers)
     UIViewController *vc = v;

     if ([v isKindOfClass:[UINavigationController class])
         vc = [v visibleViewController];

     if ([vc isKindOfClass:[MyViewController class]])
          MyViewController *myViewController = vc;
          [vc doSomething];

Example of [UITabBarController viewControllers].
//create a UITabBarController object
UITabBarController *tabBarController=[[UITabBarController alloc]init];

//FirstViewController and SecondViewController are the view controllers you want on your UITabBarController (Number of view controllers can be according to your need)
FirstViewController *firstViewController=[[FirstViewController alloc]initWithNibName:@"FirstViewController" bundle:nil];
SecondViewController *secondViewController=[[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];

//adding view controllers to your tabBarController bundling them in an array
tabBarController.viewControllers=[NSArray arrayWithObjects:firstViewController,secondViewController, nil];

//navigating to the UITabBarController that you created
[self.navigationController pushViewController:tabBarController animated:YES];

UITabBarController viewControllers example.
tabBarObj=[[UITabBarController alloc]init]; //your tabBarobj in .h file
objFirstViewCtr=[[MyFirstViewController alloc] init]; // your view controller object in .h File
must be #import "MyFirstViewController.h"
objSecondViewCtr=[[MySecondViewController alloc] init]; // same way your second viewobj
UINavigationController *v1=[[[UINavigationController alloc] initWithRootViewController: objFirstViewCtr] autorelease];
UINavigationController *v2=[[[UINavigationController alloc] initWithRootViewController: objSecondViewCtr] autorelease];

tabBarObj.viewControllers=[NSArray arrayWithObjects:v1,v2,nil];

[self.view addSubView:tabBarObj.View];

End of UITabBarController viewControllers example article.