Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

self.tabBarItem.title not working?

In my iPhone application, I have a tab bar. This tab bar holds a UINavigationController. I have:

  • In Interface Builder set the tab item title to 'Create New'
  • In the UINavigation controller I have self.tabBarItem.title = 'Create New'; and self.title = 'Create New';
  • In the UIViewController pushed onto the controller: self.tabBarItem.title = 'Create New'; but self.title = 'Blah';.

But, always, the self.title of the first view controller pushed onto the navigation controller is shown (Blah). How would you set the title of the tab bar item? Thanks, Isaac Waller

like image 926
Isaac Waller Avatar asked Apr 04 '09 21:04

Isaac Waller


2 Answers

I found if I used self.navigationItem.title = 'Blah'; instead of self.title, it would work.

like image 147
Isaac Waller Avatar answered Nov 05 '22 00:11

Isaac Waller


I just solved the same issue. I was using self.tabBarItem.item for the 1st tab of my tabBarController, which is the first and only one that loads its navigationController. So for the first tab in a tab bar you have to set the title differently. I'll try to illustrate:

tab bar with 3 tabs (a UINavigationController provides content each tab)

  1. tab 1
    • loads tab1NavController.m (sets self.tabBarItem.item to '1st')
    • loads tab1ViewController.m (sets self.title to 'tab1 View'
  2. tab 2
    • loads tab2NavController.m (sets self.tabBarItem.item to '2nd')
    • loads tab2ViewController.m (sets self.title to 'tab View'
  3. tab 3
    • loads tab3NavController.m (sets self.tabBarItem.item to '3rd')
    • loads tab3ViewController.m (sets self.title to 'tab3 View'

When the tab bar loads all of its viewControllers for each of the 3 tabs, the tab buttons in the tab bar have these labels:

  1. 'tab1 View'
  2. '2nd'
  3. '3rd'

This is because the 2nd and 3rd nav controllers aren't loaded until the user selects those tabs. The 1st tab is loaded when the UITabBarController is loaded and due to the order of events it replaces the tabBarItem.title with the navController's rootViewController's self.title.

Solution

To fix this, you simply use self.navigationItem.title instead of self.title. You ONLY have to do this for the 1st tab's navController's rootViewController.

I hope that makes sense. You did solve your own problem, but I wanted you and anyone else to know WHY it works like this.

like image 18
Brenden Avatar answered Nov 05 '22 00:11

Brenden