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

前端 未结 10 1365
猫巷女王i
猫巷女王i 2021-01-31 15:46

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

- (IBAction)tab1Click:(id)sender {
    myTabBarController = [[UITabBarContro         


        
相关标签:
10条回答
  • 2021-01-31 16:00

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

    0 讨论(0)
  • 2021-01-31 16:00

    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"
    
    0 讨论(0)
  • 2021-01-31 16:10

    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
    
    0 讨论(0)
  • 2021-01-31 16:11

    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"];
    
    0 讨论(0)
  • 2021-01-31 16:14

    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;
    
    0 讨论(0)
  • 2021-01-31 16:18

    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"];
    
    0 讨论(0)
提交回复
热议问题