Is it possible to pop the UINavigationController t

2019-06-17 00:52发布

This question already has an answer here:

Is it possible to pop the navigation controller twice? I have this navigation structure:

View One ----> View Two -----> View Three

What I'd like to accomplish is that by the tap of a row on View Three, go back directly to View One. I've done it from Three to Two via protocol-delegate, but setting the delegate in view One doesn't work and setting two consecutive delegate-protocol both poping the navigation controller, gives me error: nested navigation controller activity (or something similar).

Any help would be appreciated. Thanks in advance!

4条回答
等我变得足够好
2楼-- · 2019-06-17 01:19

Use the following code. You can use any number instead of -3 to pop to a different level.

Obj-C:

ViewController *View = [self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count-3];
    [self.navigationController popToViewController:View animated:YES];

Swift 3.0*:

let controller = self.navigationController?.viewControllers[(self.navigationController?.viewControllers.count)! - 3]        
self.navigationController?.popToViewController(controller!, animated: true)
查看更多
我想做一个坏孩纸
3楼-- · 2019-06-17 01:21

There is a few pop options

- (UIViewController *)popViewControllerAnimated:(BOOL)animated
- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated
- (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated
  • The first pops the top controller.
  • The second allows you to pop the whole stack off to get to the root.
  • The third allows you to pop to any viewController you have a reference to. You can get the viewController with self.navigationController.viewControllers and then work with the array to get the specific viewController you want to pop to
查看更多
对你真心纯属浪费
4楼-- · 2019-06-17 01:27

You can try this

[self.navigationController popToViewController:[[self.navigationController viewControllers] objectAtIndex:1] animated:YES];

Hope it Helps!!

查看更多
Root(大扎)
5楼-- · 2019-06-17 01:32

'self' seems to be released after first pop

UINavigationController *navigationController = self.navigationController;
[navigationController popViewControllerAnimated:NO];
[navigationController popViewControllerAnimated:YES];
查看更多
登录 后发表回答