5

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.

Rui Peres
  • 25,741
  • 9
  • 87
  • 137
Ryan Sullivan
  • 93
  • 1
  • 1
  • 6

6 Answers6

19
thing.navigationItem.title = sub.title;

or in StoriesViewController.m file in viewDidLoad method:

self.navigationItem.title = sub.title
jamapag
  • 9,290
  • 4
  • 34
  • 28
3

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";
circuitlego
  • 3,469
  • 5
  • 22
  • 22
1

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.

Lou Franco
  • 87,846
  • 14
  • 132
  • 192
0
[thing setTitle: sub.title];
John Franklin
  • 4,962
  • 1
  • 23
  • 27
0

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

Luke
  • 11,426
  • 43
  • 60
  • 69
Ryan Sullivan
  • 93
  • 1
  • 1
  • 6
0

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

self.tabBarController.title = sub.title;
danvk
  • 15,863
  • 5
  • 72
  • 116