0

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: https://i.stack.imgur.com/zo75V.png

And After I choose the "New Account": https://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

Community
  • 1
  • 1
  • You want to set the title `Transaction` to the DetailViewController? – soryngod Jul 16 '13 at 20:56
  • No, I want to set the title that matches the account name to my TransactionViewController, and the DetailViewController should stay unchanged – Nicola Buescher Jul 16 '13 at 21:02
  • the title in this case would be "New Account" – Nicola Buescher Jul 16 '13 at 21:09
  • Another solution I can think of is accessing the specific Account object that the table cell represents from within the TransactionViewController, if that's possible. – Nicola Buescher Jul 16 '13 at 21:26
  • a view controller inherits it's navigation bar from it's navigation controller so generally self.navigationItem should get that item, but in case it doesn't; try accessing the navigation controller, [self.navigationController.navigationItem.titleLabel setTitle:@""]; there could be a variable reasons why the title is not being set. – A'sa Dickens Jul 16 '13 at 21:30

2 Answers2

2

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, i.e. 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;
    }
}
Caleb
  • 124,013
  • 19
  • 183
  • 272
  • The `transactionViewController` is a property in my master view controller and is of type `TransactionViewController *`. How can I make sure that I have the right view controller? – Nicola Buescher Jul 16 '13 at 21:04
  • Everything you've described is correct except that I use storyboard and use segues from the table cells to push the new view controller. Does that make a big difference? I've set the title in interface builder, but I want to change it dynamically at runtime. – Nicola Buescher Jul 16 '13 at 21:14
  • @NicolaLucasBuescher Okay, I've updated the answer with some more information about how to do this with storyboards. – Caleb Jul 16 '13 at 22:01
  • Thanks for the advice. I am setting the title in the parent view, and did end up using the `-prepareForSeque:sender:` method – Nicola Buescher Jul 16 '13 at 22:30
2

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.

soryngod
  • 1,827
  • 1
  • 14
  • 13