How to check viewcontroller is added in stack or not

后端 未结 10 751
北恋
北恋 2020-12-28 08:50

I have two view controllers. I have navigated from one view to another view by press the button to using below code.

 *let secondViewController = self.storyb         


        
10条回答
  •  Happy的楠姐
    2020-12-28 09:28

    All you need is to check with nibName of the viewController and you have your result

    this code below will make sure you don't add one viewController again in navigatoinController,

        var VC = YourViewController
        var objVC = (storyboard.instantiateViewController(withIdentifier: "YourViewController"))
        
    if navigationController.viewControllers.contains(where: { (VC) -> Bool in
                return objVC.nibName == VC.nibName
            }){
                for (i,controller) in navigationController.viewControllers.enumerated()
                {
                    if controller.isKind(of: VC){
                        navigationController.viewControllers.remove(at: i)
                        navigationController.pushViewController(objVC, animated: false)
                    }
                }
            }else{navigationController.pushViewController(objVC, animated: false)}
    

    and If you just want to check if the viewController is in NaviagtionController then try this:

    var checkForView = navigationController.viewControllers.contains(where: { (VC) -> Bool in
                return objVC.nibName == VC.nibName
            })
    

提交回复
热议问题