简体   繁体   中英

How do I set the title of the NavigationItem/NavigationBar

I've been working on setting up a UI in Xcode that works similar to the iOS mail app, but the second popover view controller won't play along. I'm trying to set the title of the "Transactions" Navigation Bar to the name of the Account, but it instead sets the title for the DetailViewController:

Before I choose an account: http://i.stack.imgur.com/zo75V.png

And After I choose the "New Account": http://i.stack.imgur.com/8DPBB.png

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    Account *account = [[self fetchedResultsController] objectAtIndexPath:indexPath];
    self.transactionViewController.navigationItem.title = account.name;
}

I use the above code to set the title, but the title is set for the wrong viewController... This same code produces no result at all for the iPhone version.

EDIT: I actually found the answer to this in this guide: Passing Data between View Controllers

The key is to use the -prepareForSegue:sender: method to get the right viewController and set its title. This also allows you to pass any data from one ViewController to another whenever you use a segue to switch between them, which I now use to set the account for transactions and get the account name to set to the seque.destinationViewController.navigationItem.title property

Thank you everyone for your help

Every view controller has a navigationItem property, and every navigation item has a title property. To set the title displayed in the navigation bar for a view controller, you can simply set a view controller's navigationItem.title , just as you're doing.

I use the above code to set the title, but the title is set for the wrong viewController

It sounds like you're setting the title correctly, but perhaps setting the title of the wrong view controller. It looks like the -tableView:didSelectRowAtIndexPath: method that you mentioned in your question is in the "master" view controller, ie the one on the left side of the screen, and that its transactionViewController property points to the "detail" view controller. From your comment, I take it that you're creating a new view controller on the "master" side and pushing it onto the navigation stack, and it's that view controller that you want to have the title "New Account" (or else the name of whatever account was chosen). If that's all correct, then you should look for the place in your code where you create that new controller and set its navigationItem.title to the appropriate string.

One important question is: what object is trying to set the new view controller's title? In your code above, you're trying to do it from the parent controller. Since you're using storyboards and the new view controller is being created by a segue, you don't have easy access to the new view controller from your ...didSelectRowAtIndexPath... method. If you want to continue to set the title in the parent, you could implement -prepareForSegue:sender: to set the title. On the other hand, any view controller should be more or less in charge of how it presents itself, so you might consider having the child view controller set its own title instead of having the parent do it. The parent is probably using -prepareForSegue:sender: already to set the account in the child view controller. Maybe the child has an account property that the parent calls to set this information? You could implement the setter for that property to set the view controller's title:

- (void)setAccount:(Account*)account
{
    if (account != _account) {
        _account = account;
        self.navigationItem.title = account.name;
    }
}

It is self.navigationItem.title = account.name; . If I see well the table is inside the MasterViewController and the master has a navigation controller as root.

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