user698200
user698200

Reputation: 399

How can I go back to the first view controller?

I want to go back to the first view controller. So, I used [self.navigationController popToRootViewControllerAnimated:NO] from the third view. But, it goes back to just the second view. Do I have to use popToViewController: animated: instead? I pushed the third view like this:

[self.view addSubview:secondController.view]; // from the first view
[self.navigationController pushViewController:thirdController animated:YES]; // from the second view

Upvotes: 0

Views: 841

Answers (2)

CristiC
CristiC

Reputation: 22698

Remove the second view, before using [self.navigationController popToRootViewControllerAnimated:NO]:

UIView *v = [self.navigationController.viewControllers objectAtIndex:1];
[v removeFromSuperview];

EDIT:
I am doing like this and works ok (I use this on my third view on the stack):

NSMutableArray *allControllers = [[NSMutableArray alloc] initWithArray:self.navigationController.viewControllers];

[allControllers removeObjectAtIndex:1];

[self.navigationController setViewControllers:allControllers animated:NO];
[allControllers release];
[self.navigationController popViewControllerAnimated:YES];

Upvotes: 2

superjessi
superjessi

Reputation: 1790

It looks like your navigationController was initiated when pushing the thirdController. Your secondController was not 'pushed' by the navigationController, it was added as a subview, which is quite different. So, when you push the thirdController from the secondController, it thinks your rootController is the secondController.

You have two options here:

  1. Change the way you are presenting the secondController to actually have the navigationController push it, or
  2. Remove the secondController from view before the thirdController is presented.

You may be able to popToViewController, as you mentioned...I'm not positive if that will work, but it's possible.

Upvotes: 0

Related Questions