Can i pop to Specific ViewController?

后端 未结 16 1844
我寻月下人不归
我寻月下人不归 2020-11-28 05:35

I am using navigation based application. I push First ViewController to Second ViewController and from Second ViewController to Third ViewController. Now I want to pop from

相关标签:
16条回答
  • 2020-11-28 06:14

    Often it is more important to do that from top of stack, so:

    - (void)popToLast:(Class)aClass
    {
        for (int i=self.navigationController.viewControllers.count-1; i>=0; i--)
        {
            UIViewController *vc = self.navigationController.viewControllers[i];
            if ([vc isKindOfClass:aClass])
            {
                [self.navigationController popToViewController:vc animated:YES];
                break;
            }
        }
    }
    

    and you call that

    popToLast:[SomeViewController class];
    
    0 讨论(0)
  • 2020-11-28 06:14

    After lots of effort someone has created swift extension of back to a particular view controller in Swift 3.0.

    extension UINavigationController {
    
        func backToViewController(viewController: Swift.AnyClass) {
    
                for element in viewControllers as Array {
                    if element.isKind(of: viewController) {
                        self.popToViewController(element, animated: true)
                    break
                }
            }
        }
    }
    

    Method calling:

     self.navigationController?.backToViewController(viewController: YourViewController.self)
    
    0 讨论(0)
  • 2020-11-28 06:14

    i have answer here. This is 100% working code for Swift > 4.X

    How can I pop specific View Controller in Swift

    0 讨论(0)
  • 2020-11-28 06:16

    Updated for Swift 3:

    used below simple code, for pop to specific view controller;

     for vc in self.navigationController!.viewControllers as Array {
              if vc.isKind(of: YourViewControllerName) {
                   self.navigationController!.popToViewController(vc, animated: true)
                   break
              }
        }
    
    0 讨论(0)
提交回复
热议问题