Showing posts with label viewControllers. Show all posts
Showing posts with label viewControllers. Show all posts

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.

Saturday, June 8, 2013

UINavigationController viewControllers example in Objective C (iOS).


UINavigationController viewControllers

The view controllers currently on the navigation stack.

@property(nonatomic, copy) NSArray *viewControllers

Discussion of [UINavigationController viewControllers]
The root view controller is at index 0 in the array, the back view controller is at index n-2, and the top controller is at index n-1, where n is the number of items in the array.

Assigning a new array of view controllers to this property is equivalent to calling the setViewControllers:animated: method with the animated parameter set to NO.

UINavigationController viewControllers example.
UINavigationControllers have a property called viewControllers as you have stated above. Since this is an array of View Controllers, referencing a specific view controller in this hierarchy is no different than accessing any other object in an array.

UIViewController *theControllerYouWant = [self.navigationController.viewControllers objectAtIndex:(theIndexOfYourViewController)];
In addition check out the View Controller's Programming Guide for iOS specifically the section called 'Modifying the Navigation Stack'.

Example of [UINavigationController viewControllers].
NSMutableArray *array = [[NSMutableArray alloc] initWithArray:self.navigationController.viewControllers];
[array removeObjectAtIndex:1];

self.navigationController.viewControllers = array;
Or just use

[self.navigationController setViewControllers:array animated:NO];

UINavigationController viewControllers example.
Pretty Simple, when about to push the thirdViewController inseat of doing a simple pushViewController do this:

NSArray * viewControllers = [self.navigationController viewControllers];
NSArray * newViewControllers = [NSArray arrayWithObjects:[viewControllers objectAtIndex:0], [viewControllers objectAtIndex:1], thirdController,nil];
[self.navigationController setViewControllers:newViewControllers];
where [viewControllers objectAtIndex:0] and [viewControllers objectAtIndex:1] are your rootViewController and your FirstViewController.

End of UINavigationController viewControllers example article.