navigationItem的正确用法

今天设置一个 UITabBarController 管理下的 UIViewController 的 self.navigationItem.title 时怎么也设置不了(UITabBarController 在 UINavigationController 管理下),于是查官方 Documentation,发现如下文字:

1
2
3
@interface UIViewController (UINavigationControllerItem)
navigationItem Property
The navigation item used to represent the view controller in a parent'€™s navigation bar. (read-only)

然后设置 self.tabBarController.navigationItem.title 就有效了
顺便提一下,导航栏是其 viewControllers 共用的,所以 设置 navigationItem 要在 viewWillAppear 里,如果更改了 navigationBar,还要记得在 viewWillDisappear 的时候恢复原状。