iOS Swift3 check nil value for ViewController Object

前端 未结 6 1782
面向向阳花
面向向阳花 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:33

    Use this code. this is helpful for you.

    let viewControllers: [UIViewController] = self.navigationController!.viewControllers
                for VC  in viewControllers  {
                    if (VC.isKind(of: HomeViewController.self)) {
                        bScreen = true
                        self.navigationController?.popToViewController(VC, animated: true)
                        break;
                    }
                }
    
                if bScreen == false
                {
                    let homeVC = HomeViewController()
                    self.navigationController?.pushViewController(homeVC, animated: false)
                }
    
    0 讨论(0)
  • 2021-01-28 09:37

    This is better to use to if let / guard for an optional value to avoid crashing.

    if let viewControllers: [UIViewController] = self.navigationController.viewControllers{
    
            for VC  in viewControllers  {
    
                if (VC.isKind(of: HomeViewController.self)) {
    
                    bScreen = true
                    self.navigationController?.popToViewController(VC, animated: true)
                }
            }
    
    if bScreen == false
            {
                let homeVC = HomeViewController()
                self.navigationController?.pushViewController(homeVC, animated: false)
            }
    }
    
    0 讨论(0)
  • 2021-01-28 09:42

    Never use directly ! until you are damn sure that it will not be nil. Replace your code as below. You can use if let or guard let to unwrap optionals.

        if let viewControllers: [UIViewController] = self.navigationController?.viewControllers {
                    for VC  in viewControllers  {
    
                        if (VC.isKind(of: ViewController.self)) {
    
                            bScreen = true
                            self.navigationController?.popToViewController(VC, animated: true)
                        }
                    }
    
                    if bScreen == false
                    {
                        let homeVC = ViewController()
                        self.navigationController?.pushViewController(homeVC, animated: false)
                    }
     } 
    else {
         // IF VC is nil
    }
    
    0 讨论(0)
  • 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
                }
    
    0 讨论(0)
  • 2021-01-28 09:47
    let getCurrentVCIndex = self.navigationController?.viewControllers.indexOf({ (viewController) -> Bool in
    
    if let _ = viewController as? HomeViewController {
        return true
    }
    return false
    })
    
    
    if  getCurrentVCIndex
     {
    let HomeVC = self.navigationController?.viewControllers[getCurrentVCIndex!] as! HomeViewController
    
    self.navigationController?.popToViewController(HomeVC, animated: true)
    }
    else
      {
     // use push
    }
    

    or use like

    if  let  HomeVC =  self.navigationController?.viewControllers.filter({$0 is HomeViewController}).first
    {
    self.navigationController?.popToViewController(HomeVC!, animated: true)
    }else
    {
      // use push
    }
    
    0 讨论(0)
  • 2021-01-28 09:51

    -- Swift 3 --

    for vc in (self.navigationController?.viewControllers)! {
         if vc is HomeViewController {
            _ = self.navigationController?.popToViewController(vc, animated: true)
         }
    }
    
    0 讨论(0)
提交回复
热议问题