Monday, June 10, 2013

UITabBarController setViewControllers animated example in Objective C (iOS).

UITabBarController setViewControllers animated

Sets the root view controllers of the tab bar controller.

- (void)setViewControllers:(NSArray *)viewControllers animated:(BOOL)animated

Parameters of [UITabBarController setViewControllers animated]
The array of custom view controllers to display in the tab bar interface. The order of the view controllers in this array corresponds to the display order in the tab bar, with the controller at index 0 representing the left-most tab, the controller at index 1 the next tab to the right, and so on.
If YES, the tab bar items for the view controllers are animated into position. If NO, changes to the tab bar items are reflected immediately.

Discussion of [UITabBarController setViewControllers animated]
When you assign a new set of view controllers at runtime, the tab bar controller removes all of the old view controllers before installing the new ones. 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.

This method also sets the value of the customizableViewControllers property to the contents of the viewControllers parameter.

UITabBarController setViewControllers animated example.
// generate an instance of the needed UIViewController
adminViewController *admin = [[adminViewController alloc] init];
admin.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Admin" image:[UIImage imageNamed:@"admin.png"] tag:5];

// get the AppDelegate instance
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

// get the »viewControllers« array of the AppDelegates UITabbarController as mutable array
NSMutableArray *viewControllersArray = [NSMutableArray arrayWithArray:[[appDelegate tabBarController] viewControllers]];
// insert the UITabbarItem of the needed UIViewController
[viewControllersArray insertObject:admin atIndex:2];

// Finally tell the app delegates UITabbarController to set the needed viewControllers
[[appDelegate tabBarController] setViewControllers:viewControllersArray animated:NO];

Example of [UITabBarController setViewControllers animated].
Subclass UITabBarController

Override the - (void) loadView method and include the following code

MyCustomViewControllerOne* ctrl1 = [[[MyCustomViewControllerOne alloc] initWithNibName@"MyViewControllerOne" bundle: nil] autorelease];
UIViewController* ctrl2 = [[[UIViewController alloc] init] autorelease];
MyCustomControllerTwo* ctrl3 = [[[UIViewController alloc] initWithObject: myObj] autorelease];

ctrl1.title = @"First tab";
ctrl2.title = @"Second tab";
ctrl3.title = @"Third tab";

ctrl1.tabBarItem.image = [UIImage imageNamed:@"tab_image1.png"];
ctrl2.tabBarItem.image = [UIImage imageNamed:@"tab_image2.png"];
ctrl3.tabBarItem.image = [UIImage imageNamed:@"tab_image3.png"];

[self setViewControllers: @[ctrl1, ctrl2, ctrl3]];

UITabBarController setViewControllers animated example.
NSMutableArray* newArray = [NSMutableArray arrayWithArray:self.tabBarController.viewControllers];
[newArray removeObjectAtIndex:0];
[self.tabBarController setViewControllers:newArray animated:YES];

End of UITabBarController setViewControllers animated example article.