How can I pop specific View Controller in Swift

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

提交回复
热议问题