Hacer sengul Akac
Hacer sengul Akac

Reputation: 683

How to set tab bar item title programmatically in objective c?

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;
}

Upvotes: 37

Views: 67359

Answers (10)

Anindya
Anindya

Reputation: 351

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"

Upvotes: 1

Aitul
Aitul

Reputation: 2992

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")

Upvotes: 66

Hiren
Hiren

Reputation: 12780

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"];

Upvotes: 2

Kuldeep Tanwar
Kuldeep Tanwar

Reputation: 3526

- (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;
}

Upvotes: 0

Simon Canil
Simon Canil

Reputation: 41

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

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

Upvotes: 1

Rob
Rob

Reputation: 5954

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

Upvotes: 29

B25Dec
B25Dec

Reputation: 2377

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"];

Upvotes: 4

Legolas
Legolas

Reputation: 12325

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

Upvotes: 8

glogic
glogic

Reputation: 4092

[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];

Upvotes: 5

Caleb
Caleb

Reputation: 124997

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;

Upvotes: 4

Related Questions