开发者

How can I go back to the first view controller?

开发者 https://www.devze.com 2023-04-04 19:45 出处:网络
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 u

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:

  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.

0

精彩评论

暂无评论...
验证码 换一张
取 消