How can I pop specific View Controller in Swift

前端 未结 16 2822
一生所求
一生所求 2020-12-08 02:21

I used the Objective-C code below to pop a specific ViewController.

for (UIViewController *controller in self.navigationController.         


        
相关标签:
16条回答
  • 2020-12-08 02:34

    Try following code:

    for controller in self.navigationController!.viewControllers as Array {
        if controller.isKind(of: ViewController.self) {
            self.navigationController!.popToViewController(controller, animated: true)
            break
        }
    }
    
    0 讨论(0)
  • 2020-12-08 02:35

    Swift 5 Answer of @PabloR is Here :

    extension UINavigationController {
    
       func backToViewController(vc: Any) {
          // iterate to find the type of vc
          for element in viewControllers as Array {
            if "\(type(of: element)).Type" == "\(type(of: vc))" {
                self.popToViewController(element, animated: true)
                break
             }
          }
       }
    
    }
    

    Usage :

    self.navigationController?.backToViewController(vc: TaskListViewController.self)
    
    0 讨论(0)
  • 2020-12-08 02:36

    I prefer a "real generic" and more functional approach.

    So I came up with following UINavigationController extension functions. You can also use the first function, for anything else, where you just need to access a specific VC in the navigation stack.


    Extensions

    extension UINavigationController {
        func getViewController<T: UIViewController>(of type: T.Type) -> UIViewController? {
            return self.viewControllers.first(where: { $0 is T })
        }
    
        func popToViewController<T: UIViewController>(of type: T.Type, animated: Bool) {
            guard let viewController = self.getViewController(of: type) else { return }
            self.popToViewController(viewController, animated: animated)
        }
    }
    

    Usage

    self.navigationController?.popToViewController(of: YourViewController.self, animated: true)
    



    This should work at least in Swift 4 and 5.

    0 讨论(0)
  • 2020-12-08 02:42

    Find your view controller from navigation stack and pop to that view controller if it exists

    for vc in self.navigationController!.viewControllers {
        if let myViewCont = vc as? VCName 
        {
            self.navigationController?.popToViewController(myViewCont, animated: true)
        }
    }
    
    0 讨论(0)
  • 2020-12-08 02:43

    Please use this below code for Swift 3.0:

     let viewControllers: [UIViewController] = self.navigationController!.viewControllers as [UIViewController];
    
    for aViewController:UIViewController in viewControllers {
                if aViewController.isKind(of: YourViewController.self) {
                    _ = self.navigationController?.popToViewController(aViewController, animated: true)
                }
            }
    
    0 讨论(0)
  • 2020-12-08 02:45

    Swift 4.0

    for controller in self.navigationController!.viewControllers as Array {
            if controller.isKind(of: DashboardVC.self) {
                _ =  self.navigationController!.popToViewController(controller, animated: true)
                break
            }
        }
    

    This is working Perfect.

    0 讨论(0)
提交回复
热议问题