Monday, June 10, 2013

UITabBarItem initWithTitle example in Objective C (iOS).


UITabBarItem initWithTitle

Creates and returns a new item using the specified properties.

- (id)initWithTitle:(NSString *)title image:(UIImage *)image tag:(NSInteger)tag

Parameters of [UITabBarItem initWithTitle]
title
The item’s title. If nil, a title is not displayed.
image
The item’s image. If nil, an image is not displayed.
The images displayed on the tab bar are derived from this image. The alpha values in the source image are used to create the unselected and selected images—opaque values are ignored. If this image is too large to fit on the tab bar, it is clipped to fit. The size of a tab bar image is typically 60 x 60 pixels. See “Custom Icon and Image Creation Guidelines” for more information about tab bar icons.
tag
The receiver’s tag, an integer that you can use to identify bar item objects in your application.

Return Value of [UITabBarItem initWithTitle]
Newly initialized item with the specified properties.

UITabBarItem initWithTitle example.
Something like this

-(void)viewDidLoad {
    [super viewDidLoad];
    UITabBarItem *tbi = [[UITabBarItem alloc] initWithTitle:yourTitle image:yourIcon tag:yourTag];
    [self setTabBarItem:tbi]
    [tbi release];
}

Example of [UITabBarItem initWithTitle].
You can do this by creating the items you want for your tab bar, adding them to an array, and then calling the UITabBar method setItems:animated:

UITabBarItem *firstItem = [UITabBarItem initWithTitle:@"First" image:firstImage tag:1];
UITabBarItem *secondItem = [UITabBarItem initWithTitle:@"Second" image:secondImage tag:2];

NSArray *itemsArray = [NSArray arrayWithItems:firstItem, secondItem, nil];

[myTabBar setItems:itemsArray animated:YES];

UITabBarItem initWithTitle example.
yourViewController.tabBarItem = [[UITabBarItem alloc]
initWithTitle:NSLocalizedString(@"Name", @"Name")
image:[UIImage imageNamed:@"tab_ yourViewController.png"]
tag:3];
The viewControllers are added to the tab bar, so the image and names should be set before the tab bar becomes visible (appDelegate if they are there on app start for instance). After that, you could use the above code to change the icon and text from the loadView or viewDidAppear within that viewController.

End of UITabBarItem initWithTitle example article.