Ryan Sullivan
Ryan Sullivan

Reputation: 103

How do I correctly set a UIViewController's navigationController title?

I am trying the following code:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
 Thing *sub = [[subscriptions objectAtIndex:indexPath.row] retain];
 StoriesViewController *thing = [[StoriesViewController alloc] initWithThing:sub];
 thing.navigationController.title = sub.title;
 [self.navigationController pushViewController:thing animated:YES];
 [thing release];
 [sub release];

 [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

I thought this is how you correctly set the title for pushing the controller. I tried thing.title however that was setting the TabBarItem's title instead.

Upvotes: 6

Views: 19486

Answers (6)

circuitlego
circuitlego

Reputation: 3479

It's possible you're pushing a UITabBarController instead of your regular view controller. In this case, the navigationItem will display the title of the current controller, the tab bar controller, even though you see another view. You would have to change the tab bar controller's title to change the text displayed above.

self.tabBarController.title = @"Your title";

Upvotes: 3

danvk
danvk

Reputation: 16955

If your UIViewController is part of a UITabController, then you can do:

self.tabBarController.title = sub.title;

Upvotes: 0

Ryan Sullivan
Ryan Sullivan

Reputation: 103

The solution was thing.navigationItem.title = @"title"; however it turns out that subclassing a UINavigationController broke it somehow and my solution to that was to use a category rather than a subclass

Upvotes: 0

John Franklin
John Franklin

Reputation: 4912

[thing setTitle: sub.title];

Upvotes: 0

Lou Franco
Lou Franco

Reputation: 89232

The title needs to be set in (or after) viewDidLoad, so if you want to set from another class that is creating StoriesViewController.

  1. Make another property and have that class set it.
  2. In StoriesViewController's viewDidLoad, set the title from that property.

This is because outlets aren't initialized to their view counterparts until viewDidLoad.

It looks like StoriesViewController is holding on to sub (does the initWithThing set a property to it?) If so, just set the title in viewDidLoad to the Thing property's title.

Upvotes: 1

jamapag
jamapag

Reputation: 9318

thing.navigationItem.title = sub.title;

or in StoriesViewController.m file in viewDidLoad method:

self.navigationItem.title = sub.title

Upvotes: 19

Related Questions