0

I've had some code working fine with this:

[self.navigationController pushViewController:subView animated:YES];

To push a new view onto a UINavigationController stack, however I've now implemented some functionality that reloads the content of a view and then updates the title, so I have a function like this:

- (void)loadEntries:(NSString *)entriesToLoad
{
   //Loading code
   self.title = [entriesToLoad lastPathComponent];
}

This code does set the title fine, however it causes the UINavigationController to lose what the main root view was. If I comment that code out it works absolutely fine, currently I just set the title when I'm about to push the new view onto the view stack and that has been working fine. To add to the strangeness if the view is a child view of a child view so (root view -> child view -> child view) it seems to treat the first child view as the root. I'm unsure why changing the title would have such an impact on the navigation stack.

Nicholas Smith
  • 11,642
  • 6
  • 37
  • 55

3 Answers3

0

There must be some confusion here. We are talking about a child view (which will be a sub-view after being added) or a child view controller? Because:

[self.navigationController pushViewController:subView animated:YES];

Is expecting a UIViewController's subclass and not a UIView's subclass.


If you want to play with subviews, you can simply:

[self.view addSubView:mySubView]; 

And then change the title of your UINavigationController.

Rui Peres
  • 25,741
  • 9
  • 87
  • 137
  • Sorry, I wasn't very clear where it says child view I mean child view controller. – Nicholas Smith Jul 18 '13 at 05:54
  • Changing the title at `viewWillAppear`should be enough. So when you get back to your rootViewController, the title will be updated again. Like so: http://stackoverflow.com/questions/3382573/how-do-i-correctly-set-a-uiviewcontrollers-navigationcontroller-title – Rui Peres Jul 18 '13 at 08:03
  • The problem being when I change it in `viewWillAppear` it loses the root view the child view controller came from. – Nicholas Smith Jul 18 '13 at 11:52
0

Doesn't sound like it's related to setting the title, perhaps something else you're doing. But I prefer setting the title on the navigationItem property so it doesn't affect other parts (such as a tabBarItem's title.

[self.navigationItem setTitle:[entriesToLoad lastPathComponent];
runmad
  • 14,846
  • 9
  • 99
  • 140
0

For anyone who has the same issue I worked out the cause.

If the root view doesn't have a title set it usually defaults to Back in the navigation back button, however if you then change the title in a child viewController managed view and the root doesn't have a set title it breaks the navigation logic. Whether that is a bug in UINavigationController or expected behaviour I'm unsure, but ensuring the root view had a title resolved the issue.

Nicholas Smith
  • 11,642
  • 6
  • 37
  • 55