37

I want to set title to tab item programatically, but it not works. My code is below:

- (IBAction)tab1Click:(id)sender {
    myTabBarController = [[UITabBarController alloc] init];        
    view2Controller = [[View2Controller alloc] init]; 
    [view2Controller setTitle:@"title"];
    view3Controller = [[View3Controller alloc] init];  
    deneme = [[ViewController alloc] init];  

    myTabBarController.viewControllers = [NSArray arrayWithObjects:deneme, view2Controller,view3Controller, nil]; 
    [self.view addSubview:myTabBarController.view];    
    myTabBarController.selectedIndex=1;
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Hacer sengul Akac
  • 683
  • 3
  • 17
  • 25

10 Answers10

66

You can set all the UITabBar icons in an easy way. You can do this in your viewWillAppear: method:

[[self.tabBarController.tabBar.items objectAtIndex:0] setTitle:NSLocalizedString(@"BotonMapas", @"comment")];

[[self.tabBarController.tabBar.items objectAtIndex:1] setTitle:NSLocalizedString(@"BotonRA", @"comment")];

[[self.tabBarController.tabBar.items objectAtIndex:2] setTitle:NSLocalizedString(@"BotonEstado", @"comment")];

[[self.tabBarController.tabBar.items objectAtIndex:3] setTitle:NSLocalizedString(@"LabelInfo", @"comment")];

Swift 3.1 Solution

self.tabBarController?.tabBar.items?[0].title = NSLocalizedString("BotonMapas", comment: "comment")
self.tabBarController?.tabBar.items?[1].title = NSLocalizedString("BotonRA", comment: "comment")
self.tabBarController?.tabBar.items?[2].title = NSLocalizedString("BotonEstado", comment: "comment")
self.tabBarController?.tabBar.items?[3].title = NSLocalizedString("LabelInfo", comment: "comment")
Sourabh Sharma
  • 8,222
  • 5
  • 68
  • 78
Aitul
  • 2,982
  • 2
  • 24
  • 52
29

How I do it in the actual View Controller (not the App Delegate):

// set tab title
self.title = @"Tab Title";
// optionally, set different title for navigation controller
self.navigationItem.title = @"Nav Title";
// Note: self.title will reset Nav Title. Use it first if you want different titles
Rob
  • 5,534
  • 1
  • 22
  • 22
  • 1
    +1 for setting a different title for the navigation controller! I needed that to make it uppercase, on the navigation bar only. – nmdias Mar 03 '15 at 16:03
8

An easy way to do this : In your viewController2 's viewDidLoad method, set self.title = @"MyTitle";

Legolas
  • 12,145
  • 12
  • 79
  • 132
  • 1
    The issue with this is that if it is in the viewDidLoad for viewcontroller2, it will only fire once viewController2's tab has been clicked on. At that point it will show correctly, but to fix it so that it shows correctly from the get-go, us: [self.tabBarController.tabBar.items objectAtIndex:1] setTitle:NSLocalizedString(@"BotonMapas", @"comment")]; Like Aitul listed as his answer. – Steven Marlowe Mar 16 '13 at 20:18
5
[view2Controller setTitle:@"ImATitle"];

might be what your after

edit: ok i just tested this and it def works for me so give it a go

UINavigationController *nav1 = [[UINavigationController alloc] init]; 
myViewController *myView = [[myViewController alloc] init];
//myView.title = @"Title"; //prob not needed
[nav1 pushViewController: myView  animated:NO];
UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"Title" image:[UIImage      imageNamed:@"title.png"] tag:0];
nav1.tabBarItem = item;
UITabBarController *tbc = [[UITabBarController alloc] init];
tbc.viewControllers = [NSArray arrayWithObjects:nav1, nil];
glogic
  • 4,042
  • 3
  • 28
  • 44
4

try this

[(UIViewController *)[tabBarController.viewControllers objectAtIndex:Index] setTitle:@"Title"]; 

or also you can set tab bar by in this way

UITabBarItem *tabItem = [[[tabBarController tabBar] items] objectAtIndex:INDEX];
[tabItem setTitle:@"TITLEe"];
Hiren
  • 12,720
  • 7
  • 52
  • 72
B25Dec
  • 2,301
  • 5
  • 31
  • 54
  • Judging from the documentation, there's no way to set the title of a tab bar item after you create it. You need to make a new one instead. – Caleb Jan 19 '12 at 12:44
4

The title displayed on a given tab bar item is determined by the corresponding view controller's instance of UITabBarItem. Those aren't mutable, though... if you want to change the title (or image, or tag), you have to make a new item and assign it to the view controller.

UITabBarItem *item2 = [[UITabBarItem alloc initWithTitle:@"someTitle" image:someImage tag:0];
viewController2.tabBarItem = item2;
Caleb
  • 124,013
  • 19
  • 183
  • 272
  • I try this but not worked - (IBAction)tab1Click:(id)sender { myTabBarController = [[UITabBarController alloc] init]; view2Controller = [[View2Controller alloc] init]; view3Controller = [[View3Controller alloc] init]; UITabBarItem *item2 = [[UITabBarItem alloc]initWithTitle:@"someTitle" image:nil tag:0]; view2Controller.tabBarItem = item2; myTabBarController.viewControllers = [NSArray arrayWithObjects:deneme,view2Controller,view3Controller, nil]; [self.view addSubview:myTabBarController.view]; myTabBarController.selectedIndex=1; } – Hacer sengul Akac Jan 19 '12 at 13:02
2

first declare UITabBarDelegate

- (IBAction)tab1Click:(id)sender {

    myTabBarController = [[UITabBarController alloc] init]; 

    myTabBarController.delegate = self;

    view2Controller = [[View2Controller alloc] init]; 
    [view2Controller setTitle:@"title"];
    view3Controller = [[View3Controller alloc] init];  
    deneme = [[ViewController alloc] init];  
    dename.title = @"Dename";
    view2Conreoller.title = @"View2";
    view3Conreoller.title = @"View3";
    myTabBarController.viewControllers = [NSArray arrayWithObjects:deneme, view2Controller,view3Controller, nil]; 
    [self.view addSubview:myTabBarController.view];    
    myTabBarController.selectedIndex=1;
}

and even you can set tab images using

view2Controller.tabBarItem.image = [UIImage imageNamed:@"misle.png"];
Meet Doshi
  • 4,241
  • 10
  • 40
  • 81
Hiren
  • 12,720
  • 7
  • 52
  • 72
1

If you are calling this from within an embedded UIViewController, you can change the parent tab bar title with:

self.tabBarController.title = @"My Title.";

1

In swift you can change the UITabBarController item's title in viewDidLoad: method as below-

   self.viewControllers![0].title="Deposit"
   self.viewControllers![1].title="Withdraw"
   self.viewControllers![2].title="Activity"
Anindya
  • 351
  • 6
  • 7
0
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {


    UITabBarController *tb=[[UITabBarController alloc]init];
    UIStoryboard *sb=[UIStoryboard storyboardWithName:@"Main"   bundle:nil];
    UIViewController *vc1=[sb   instantiateViewControllerWithIdentifier:@"View1"];
    UIViewController *vc2=[sb   instantiateViewControllerWithIdentifier:@"View2"];

      //To Set Title for UIViewController

      [vc1 setTitle:@"View1"];
      [vc2 setTitle:@"View2"];


    NSArray *vController=[[NSArray alloc]initWithObjects:vc1,vc2,nil];
    tb.viewControllers=vController;
    self.window.rootViewController=tb;
    [self.window makeKeyAndVisible];


  return YES;
}
tryKuldeepTanwar
  • 3,490
  • 2
  • 19
  • 49