Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Title of UITabBar

I am creating and adding UITabBarController programatically in my App Delegate.

I have 5 view controllers in my tab bar that means 5 views.

I want to set title of different tabs from contrller.

Please help me to do it.

Thanks

like image 202
rkb Avatar asked Jul 16 '09 16:07

rkb


2 Answers

NSArray *tabBarItemTitles = [NSArray arrayWithObjects: @"Title1", @"Title2", @"Title3", nil];

for (UIViewController *viewController in self.viewControllers)
{
    viewController.title = [tabBarItemTitles objectAtIndex: [self.viewControllers indexOfObject: viewController]];
}

This sets the title of each of the view controllers (the title shown at the top in the navigation controller) to the matching title in the tabBarItemTitles array.

If you're trying to set the text in the tabBarItem, do this:

NSArray *tabBarItemTitles = [NSArray arrayWithObjects: @"Title1", @"Title2", @"Title3", nil];

for (UItabBarItem *item in self.items)
{
    item.title = [tabBarItemTitles objectAtIndex: [self.items indexOfObject: item]];
}
like image 93
Dan Lorenc Avatar answered Oct 14 '22 11:10

Dan Lorenc


The title shown for each tab in the tab bar generally corresponds to the title of the corresponding ViewController.

For example, if I have a tabBarController with five tabs, then that means I have 5 view controllers in my tabBarController. The title of the first tab will be the title property of the first view controller, etc. In other words, if you've done this,

[myFirstViewController setTitle:@"First"];

then "First" will be the title of your tab.

You can also manipulate the title directly by retrieving the UITabBarItem (a subclass of UIBarItem) and setting the title yourself, but this is usually only necessary if one of your viewController titles is too long to display properly.

like image 40
Dave DeLong Avatar answered Oct 14 '22 12:10

Dave DeLong