Showing posts with label UITabBarController example. Show all posts
Showing posts with label UITabBarController example. Show all posts

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]
viewControllers
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.
animated
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.

UITabBarController setViewControllers example in Objective C (iOS).


UITabBarController setViewControllers

Sets the root view controllers of the tab bar controller.

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

Parameters of [UITabBarController setViewControllers]
viewControllers
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.
animated
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]
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 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].
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 example.
NSMutableArray* newArray = [NSMutableArray arrayWithArray:self.tabBarController.viewControllers];
[newArray removeObjectAtIndex:0];
[self.tabBarController setViewControllers:newArray animated:YES];

End of UITabBarController setViewControllers example article.

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.

UITabBarController selectedViewController example in Objective C (iOS).


UITabBarController selectedViewController

The view controller associated with the currently selected tab item.

@property(nonatomic, assign) UIViewController *selectedViewController

Discussion of [UITabBarController selectedViewController]
This view controller is the one whose custom view is currently displayed by the tab bar interface. The specified view controller must be in the viewControllers array. Assigning a new view controller to this property changes the currently displayed view and also selects an appropriate tab in the tab bar. Changing the view controller also updates the selectedIndex property accordingly. The default value of this property is nil.[UITabBarController selectedViewController]

In iOS 3.0 and later, you can use this property to select any of the view controllers in the viewControllers property. This includes view controllers that are managed by the More navigation controller and whose tab bar items are not visible in the tab bar. You can also use it to select the More navigation controller itself, which is available from the moreNavigationController property. Prior to iOS 3.0, you could select only the More navigation controller and the subset of view controllers whose tab bar item was visible. Attempting to set this property to a view controller whose tab bar item was not visible had no effect.

UITabBarController selectedViewController example.
if ([[WSFUserDefaults sharedInstance] savedTabBarLocation] > 0) {

     if ([[WSFUserDefaults sharedInstance] savedTabBarLocation] > 3) {
     UIViewController *selectViewController = [tabBarController.viewControllers objectAtIndex:[[WSFUserDefaults sharedInstance] savedTabBarLocation]];
     [tabBarController setSelectedViewController:tabBarController.moreNavigationController];
     [tabBarController.moreNavigationController popToRootViewControllerAnimated:NO];//make sure we're at the top level More
     [tabBarController.moreNavigationController pushViewController:selectViewController animated:NO];
     }
     else {
     [tabBarController setSelectedIndex:[[WSFUserDefaults sharedInstance] savedTabBarLocation]];
     }
     }

Example of [UITabBarController selectedViewController].
        DefaultView *defaultView = [[DefaultView alloc]initWithNibName:@"DefaultView" bundle:[NSBundle mainBundle]];
        [self.tabBarController setSelectedViewController:nil];
        [self.tabBarController setSelectedViewController:defaultView];

UITabBarController selectedViewController example.
- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Create the view controller which will be displayed after application startup
    mHomeViewController = [[HomeViewController alloc] initWithNibName:nil bundle:nil];

    [tabBarController.view addSubview:mHomeViewController.view];
    tabBarController.delegate = self;
    [tabBarController addObserver:self forKeyPath:@"selectedViewController" options:NSKeyValueObservingOptionNew context:NULL];

    // further initialization ...
}

// This method detects if user taps on one of the tabs and removes our "Home" view controller from the screen.
- (BOOL) tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    if (!mAllowSelectTab)
    {
        [mHomeViewController.view removeFromSuperview];
        mAllowSelectTab = YES;
    }

    return YES;
}

// Here we detect if UITabBarController wants to select one of the tabs and set back to unselected.
- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if (!mAllowSelectTab)
    {
        if (object == tabBarController && [keyPath isEqualToString:@"selectedViewController"])
        {
            NSNumber *changeKind = [change objectForKey:NSKeyValueChangeKindKey];

            if ([changeKind intValue] == NSKeyValueChangeSetting)
            {
                NSObject *newValue = [change objectForKey:NSKeyValueChangeNewKey];

                if ([newValue class] != [NSNull class])
                {
                    tabBarController.selectedViewController = nil;
                }
            }
        }
    }
}

End of UITabBarController selectedViewController example article.

UITabBarController selectedIndex example in Objective C (iOS).


UITabBarController selectedIndex

The index of the view controller associated with the currently selected tab item.

@property(nonatomic) NSUInteger selectedIndex

Discussion of [UITabBarController selectedIndex]
This property nominally represents an index into the array of the viewControllers property. However, if the selected view controller is currently the More navigation controller, this property contains the value NSNotFound. Setting this property changes the selected view controller to the one at the designated index in the viewControllers array. To select the More navigation controller itself, you must change the value of the selectedViewController property instead.[UITabBarController selectedIndex]

In versions of iOS prior to version 3.0, this property reflects the index of the selected tab bar item only. Attempting to set this value to an index of a view controller that is not visible in the tab bar, but is instead managed by the More navigation controller, has no effect.

UITabBarController selectedIndex example.
int index = tabBarController.selectedIndex;
if (tabBarController.selectedViewController ==
    tabBarController.moreNavigationController) {
    index = -1;  //assign some placeholder index for the "More" controller
}

Example of [UITabBarController selectedIndex].
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
        {
          NSUserDefaults *def = [NSUserDefaults standardUserDefaults];

          [def setInteger: [NSNumber numberWithInt: tabBarController.selectedIndex]
 forKey:@"activeTab"];

          [def synchronize];
        }

UITabBarController selectedIndex example.
- (void)applicationDidFinishLaunchingUIApplication *)application { 
   NSUserDefaults *def = [NSUserDefaults standardUserDefaults];

   int activeTab = [(NSNumber*)[def objectForKey:@"activeTab"] intValue];

   tabBarController.selectedIndex = activeTab;
}

End of UITabBarController selectedIndex example article.

UITabBarController moreNavigationController example in Objective C (iOS).


UITabBarController moreNavigationController

The view controller that manages the More navigation interface. (read-only)

@property(nonatomic, readonly) UINavigationController *moreNavigationController

Discussion of [UITabBarController moreNavigationController]
This property always contains a valid More navigation controller, even if a More button is not displayed on the screen. You can use the value of this property to select the More navigation controller in the tab bar interface or to compare it against the currently selected view controller.[UITabBarController moreNavigationController]

Do not add the object stored in this property to your tab bar interface manually. The More controller is displayed automatically by the tab bar controller as it is needed. You must also not look for the More navigation controller in the array of view controllers stored in the viewControllers property. The tab bar controller does not include the More navigation controller in that array of objects.

UITabBarController moreNavigationController example.
UIViewController *tbMore =
    ((UIViewController*)
    [self.moreNavigationController.viewControllers objectAtIndex:0]);

int nRows = [((UITableView *)tbMore.view) numberOfRowsInSection:0];

for (int i = 0; i < nRows; i++)
{
    UITableViewCell *c =
        [((UITableView *)tbMore.view)
        cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];

    // Do any additional customization here!
}

Example of [UITabBarController moreNavigationController].
int index = tabBarController.selectedIndex;
if (tabBarController.selectedViewController ==
    tabBarController.moreNavigationController) {
    index = -1;  //assign some placeholder index for the "More" controller
}

UITabBarController moreNavigationController example.
@interface MyDelegate : NSObject <UITabBarControllerDelegate, UINavigationControllerDelegate>
{
}
Wherever you are assigning your class as delegate, do this:

- (void) assignDelegate:(MyDelegate)myDelegate toTabBarController:(UITabBarController*)tabBarController
{
  tabBarController.delegate = myDelegate;
  tabBarController.moreNavigationController.delegate = myDelegate;
}
And finally, in your delegate class add this method:

- (void) navigationController:(UINavigationController*)navigationController didShowViewController:(UIViewController*)viewController animated:(BOOL)animated
{
     // add code to handle the event
}

End of UITabBarController moreNavigationController example article.

UITabBarController customizableViewControllers example in Objective C (iOS).


UITabBarController customizableViewControllers

The subset of view controllers managed by this tab bar controller that can be customized.

@property(nonatomic, copy) NSArray *customizableViewControllers

Discussion of [UITabBarController customizableViewControllers]
This property controls which items in the tab bar can be rearranged by the user. When the user taps the More item on the tab bar view, a custom interface appears displaying any items that did not fit on the main tab bar. This interface also contains an Edit button that allows the user to rearrange the items. [UITabBarController customizableViewControllers] Only the items whose associated view controllers are in this array can be rearranged from this interface. If the array is empty or the value of this property is nil, the tab bar does not allow any items to be rearranged.

Changing the value of the viewControllers property (either directly or using the setViewControllers:animated: method) also changes the value of this property. When first assigned to the tab bar controller, all view controllers are customizable by default.

UITabBarController customizableViewControllers example.
Set tabBarController.customizableViewControllers to something, the easiest case would be:

tabBarController.customizableViewControllers = tabBarController.viewControllers;
after you set up the viewControllers.


Example of [UITabBarController customizableViewControllers].
#import <UIKit/UIKit.h>

@interface CustomTabBarController : UITabBarController {

}
@end

#import "CustomTabBarController.h"

@implementation CustomTabBarController

- (void)viewDidLoad
{
    self.customizableViewControllers = nil;
    [super viewDidLoad];
}  

@end

UITabBarController customizableViewControllers example.
// 'Move view' customization
// MORE tab bar items 'More view'
- (void)applicationDidFinishLaunching:(UIApplication *)application {
  self.tabBarController.customizableViewControllers=nil;

  UIViewController  * moreController =
  [[self.tabBarController.moreNavigationController viewControllers] objectAtIndex:0] ;
  UITableView * moreTableView = ( UITableView *)  [moreController view];
  [moreTableView setSeparatorColor:[UIColor redColor]];
  [moreTableView setBackgroundColor:[UIColor yellowColor]];
}

End of UITabBarController customizableViewControllers example article.