Can i pop to Specific ViewController?

后端 未结 16 1845
我寻月下人不归
我寻月下人不归 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:03
    for controller in self.navigationController!.viewControllers as Array {
            if controller.isKind(of: LoginVC.self) {
                _ =  self.navigationController!.popToViewController(controller, animated: true)
                break
            }
        }
    
    0 讨论(0)
  • 2020-11-28 06:05
    - (void) RetunToSpecificViewController{
    
       for (UIViewController *controller in self.navigationController.viewControllers)
        {
               if ([controller isKindOfClass:[AnOldViewController class]])
               {
                //Do not forget to import AnOldViewController.h
    
                           [self.navigationController popToViewController:controller
                                                                 animated:YES];
                            break;
                }
        }
    }
    
    0 讨论(0)
  • 2020-11-28 06:09

    Put function in UIViewController 1. it checks if Specific UIViewController exists In UINavigationController then popToViewController or else pushViewController

    func navigate(_ navVC: AnyClass, pushVC: UIViewController) {
        for obj in self.navigationController!.viewControllers {
            if obj.isMember(of: navVC) {
                self.navigationController!.popToViewController(obj, animated: true)
                return
            }
        }
        self.navigationController!.pushViewController(pushVC, animated: true)
    }
    

    Use

    self.navigate(ViewController.self, pushVC: self.storyboard?.instantiateViewController(withIdentifier: "ViewController") as! ViewController)
    
    0 讨论(0)
  • 2020-11-28 06:10

    Quick and safe Swift 3 version:

    if let vc = navigationController.viewControllers.filter({ $0 is SpecificViewControllerClass }).first {
      navigationController.popToViewController(vc, animated: true)
    }
    
    0 讨论(0)
  • 2020-11-28 06:12

    By Writing the First Line you get the Indexes of all View Controllers and from second Line You will reach up to your Destination.

    NSArray *array = [self.navigationController viewControllers];
    
    [self.navigationController popToViewController:[array objectAtIndex:2] animated:YES];
    
    0 讨论(0)
  • 2020-11-28 06:12

    A safer approach:

    - (void)turnBackToAnOldViewController{
    
        for (UIViewController *controller in self.navigationController.viewControllers) {
    
            //Do not forget to import AnOldViewController.h
            if ([controller isKindOfClass:[AnOldViewController class]]) { 
    
                [self.navigationController popToViewController:controller
                                                      animated:YES];
                return;
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题