简体   繁体   中英

Change title of navigationItem Title based on selected Tab Bar Item

在此输入图像描述 I have a Tab Bar View Controller that has four possible view controllers. My main tab bar view that all the other views have a relationship too has a navigation bar that i can double click on and change the title in my storyboard. What i need to know is how to changed the title of this navigation item based on the selected tab bar item.

For instance i will have four tabs. 1 is "Bills", 2 is "Groceries", 3 is "Gas", and 4 is "Personal". I want the title of my view to be Bills if the "Bills Tab is selected and so on.

EDIT: Hopefully this will clarify what i'm trying to accomplish.

I want to be able to change the title of the Navigation Bar at the top of each of the four table Views on the right. The Tab Bar Controller is the only place that I can actually double click and change the tile while is storyboard. I want the title to change based on which table view is selected from the tab bar.

You should just be able to set the title property within each of the "four view controllers" like this:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"Title For View Controller Goes Here";
}

Then, whenever said tab for this view controller is clicked, the navigation bar's title should change to this title.

Edit

It sounds like you may have placed a UINavigationBar object from Interface Builder in your tab bar's view. Instead, you actually want to have the UITabBarController embedded within a UINavigationController .

Go through Ray Wenderlich's tutorial on starting storyboarding, and this should show you how to do this:

http://www.raywenderlich.com/5138/beginning-storyboards-in-ios-5-part-1

Good luck!

On every tab selection you can change title.

   - (void)tabBarController:(UITabBarController *)tabBarController 
     didSelectViewController:(UIViewController *)viewController
    {
      viewController.title=@"your title"
    }

I should warn you that this method may be an inelegant hack, because I'm only just beginning to understand navigation myself. But here's how I would approach it:

Basically, you should add a UINavigationItem to each of the UIViewController s you want to have a title, and change the text of the UINavigationItem depending on which UIViewController is showing.

Remove any navigation bar item you manually added to the view controller and add this to your viewDidLoad method

self.navigationController.navigationBar.topItem.title = @"My Title";

or

self.navigationController.topViewController.title = @"My Title";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM