How can I pop specific View Controller in Swift

前端 未结 16 2967
一生所求
一生所求 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:53

    Swift 5

    To pop to the latest instance of a specific class, for example SomeViewController:

    navigationController?.popToViewController(ofClass: SomeViewController.self)
    

    But you need to add ths UINavigationController extension:

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

提交回复
热议问题