iOS Swift3检查ViewController对象的nil值 [英] iOS Swift3 check nil value for ViewController Object

查看:138
本文介绍了iOS Swift3检查ViewController对象的nil值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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)
}

我循环通过导航控制器阵列移动到HomeViewController.above代码工作正常。有些时候我正在崩溃,因为致命错误:在打开一个可选的时候意外地发现了nil值得。我知道这次崩溃的原因。请帮我查看视图控制器对象的nil值。我们将非常感谢你们的帮助。谢谢你们。

I loop through navigation controller array to move to HomeViewController.above code is working fine.some times i am getting crash as "fatal error: unexpectedly found nil while unwrapping an Optional value".I know the cause for this crash.Please help me how to check nil value for view controller object.any help will be appreciated.thanks in advance

推荐答案

使用此代码。这对你很有帮助。

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)
            }

这篇关于iOS Swift3检查ViewController对象的nil值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆