How to check viewcontroller is added in stack or not

后端 未结 10 782
北恋
北恋 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:22

    To check whether the navigation stack contains a particular type of view controller, you can use:

    if let viewControllers = self.navigationController?.viewControllers
    {
        if viewControllers.contains(where: {
            return $0 is YourViewController
        })
        {
            //Write your code here
        }
    }
    

    To remove a particular controller from navigation stack, you need to make changes to the navigation stack.

    Example:

        if var viewControllers = self.navigationController?.viewControllers
        {
            for controller in viewControllers
            {
                if controller is UIViewController
                {
                    viewControllers.removeElement(controller)
                    self.navigationController?.viewControllers = viewControllers
                }
            }
        }
    

提交回复
热议问题