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
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]];
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With