How can I pop specific View Controller in Swift

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

    I have added an extension to UINavigationController which helps you to find if that controller exist in navigation stack. If yes then it will be popped to that controller or else you pass new controller to push with pushController param.

    extension UINavigationController {
    
        func containsViewController(ofKind kind: AnyClass) -> Bool {
            return self.viewControllers.contains(where: { $0.isKind(of: kind) })
        }
    
        func popPushToVC(ofKind kind: AnyClass, pushController: UIViewController) {
            if containsViewController(ofKind: kind) {
                for controller in self.viewControllers {
                    if controller.isKind(of: kind) {
                        popToViewController(controller, animated: true)
                        break
                    }
                }
            } else {
                pushViewController(pushController, animated: true)
            }
        }
    }
    

提交回复
热议问题