简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM