self.tabBarItem.title not working?

家住魔仙堡 提交于 2019-11-28 23:32:26

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!