How do I pop two views at once from a navigation c

2019-01-16 03:07发布

I want to pop to the third view on the navigation stack back to the first view.

I know how to pop one view at once:

[self.navigationController popViewControllerAnimated:YES];

But how do I do two at once?

Thanks...

16条回答
Emotional °昔
2楼-- · 2019-01-16 03:16

you can pop back to the root view controller

[self.navigationController popToRootViewControllerAnimated:YES];

or, if the view you want to pop to isn't the first one, you'll need to pop again in your previous view's viewWillAppear

查看更多
Evening l夕情丶
3楼-- · 2019-01-16 03:17

In Swift 3, you can pop one, two or more viewcontrollers from navigation controller like that

let viewControllers = self.navigationController!.viewControllers as [UIViewController]
    for aViewController:UIViewController in viewControllers {
        if aViewController.isKind(of: FromWhereYouWantToGoController.self) {
            _ = self.navigationController?.popToViewController(aViewController, animated: true)
        }
    }

Here FromWhereYouWantToGoController is destination controller. Hope it helps.

查看更多
我命由我不由天
4楼-- · 2019-01-16 03:21

Here are two UINavigationController extensions that can solve your problem. I would recommend using the first one that pops to a UIViewController of a specific class:

extension UINavigationController {

  func popToViewController(ofClass: AnyClass, animated: Bool = true) {
    if let vc = viewControllers.filter({$0.isKind(of: ofClass)}).last {
      popToViewController(vc, animated: animated)
    }
  }

  func popViewControllers(viewsToPop: Int, animated: Bool = true) {
    if viewControllers.count > viewsToPop {
      let vc = viewControllers[viewControllers.count - viewsToPop - 1]
      popToViewController(vc, animated: animated)
    }
  }

}

and use it like this:

// pop to SomeViewController class
navigationController?.popToViewController(ofClass: SomeViewController.self)

// pop 2 view controllers
navigationController?.popViewControllers(viewsToPop: 2)
查看更多
再贱就再见
5楼-- · 2019-01-16 03:29

Here's a little function I'm using to go back X ViewControllers:

-(void)backMultiple:(id)data {
    int back = 2; //Default to go back 2 
    int count = [self.navigationController.viewControllers count];

    if(data[@"count"]) back = [data[@"count"] intValue];

    //If we want to go back more than those that actually exist, just go to the root
    if(back+1 > count) {
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
    //Otherwise go back X ViewControllers 
    else {
        [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:count-(back+1)] animated:YES];
    }
}
查看更多
孤傲高冷的网名
6楼-- · 2019-01-16 03:30

You can pass the initial view controller (the one you want to come back to) and then call this line in the last view:

[self.navigationController popToViewController:yourInitialViewController animated:YES];

L.

查看更多
▲ chillily
7楼-- · 2019-01-16 03:33

I didn't see this answer in here. If you only want to pop a few (not all the way to the root), you can just iterate through self.navigationController.viewControllers checking for class types, or if you have a reference use that:

for (UIViewController *aViewController in self.navigationController.viewControllers) {
   if ([aViewController isKindOfClass:[SMThumbnailViewController class]]) {
      [self.navigationController popToViewController:aViewController animated:YES];
   }
}
查看更多
登录 后发表回答