Dailycode.info

Short solution for short problems

iOS: Change Navigation Item text inside a Tab Bar Controller

I have an application that uses a navigation controller. After a login screen it navigates to a tab bar controller. The problem was the when tab bars where pressed, the navigation item text was not changed. It show the navigation item text that was set on the tab bar controller.

 

So the way to solve this was to implement a custom UITabBarController. Assign this to your TabBarController as class. Then in the implementation of the custom tab bar controller, you can change the navigation item text according to the tab bar item that was chosen:

- (void)viewDidLoad

{

    [superviewDidLoad];

// Do any additional setup after loading the view.

    self.navigationItem.title = [self.tabBar.items[0] title];

}

 

- (void)didReceiveMemoryWarning

{

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

- (void) tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item

{

    self.navigationItem.title = item.title;

}