iOS Swift3 check nil value for ViewController Object

前端 未结 6 1835
面向向阳花
面向向阳花 2021-01-28 09:16
let viewControllers: [UIViewController] = self.navigationController!.viewControllers

for VC  in viewControllers  {            
    if (VC.isKind(of: HomeViewController.         


        
6条回答
  •  独厮守ぢ
    2021-01-28 09:45

    Based on your code, In the loop, if the navigation stack contains the respective view controller will be popped to the respective page. But the thing is if the same view controller is present two times, will lead to execute the loop for the same time. This may cause crash. So Add a break after the poptoviewcontroller will avoid this issue. Please check the below code, will help you.

     if (VC.isKind(of: HomeViewController.self)) {
    
                    bScreen = true
                    self.navigationController?.popToViewController(VC, animated: true)
                    break
                }
    

提交回复
热议问题