Can i pop to Specific ViewController?

后端 未结 16 1931
我寻月下人不归
我寻月下人不归 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    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)
    

提交回复
热议问题