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?开发者_如何学Python
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
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];
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:
- Change the way you are presenting the secondController to actually have the navigationController push it, or
- 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.
精彩评论