How to check viewcontroller is added in stack or not

后端 未结 10 740
北恋
北恋 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条回答
  •  执笔经年
    2020-12-28 09:23

    Here we go.

    This line will give you a array of UIViewControllers

    self.navigationController?.viewControllers

    Now what you have to do is check your viewControllerObject does exist or not?

    By writing this line

    if viewController.isKindOfClass(YourController){
    }
    

    and here is a complete code.

    if let viewControllers = self.navigationController?.viewControllers {
                for viewController in viewControllers {
    
                    if viewController.isKindOfClass(YourController) {
                        print("Your controller exist")
                    }
           } 
    }
    

    When you write below line while going back to your 'ViewControllerA' it will remove a ViewControllerB from navigation stack.

    self.navigationController?.popViewControllerAnimated(true)
    

    It is just similar pop operation which we are doing with stack and navigationcontroller is a stack.

    Let me know if you have any confusions.

提交回复
热议问题