ios - 推送导航 Controller 时出现异常 - iOS

标签 ios swift5

为什么我在导航时遇到异常。我是iOS开发的初学者。用户登录后我正在导航。我使用了以下代码。我不明白我错过了什么。

我在最后一行收到以下异常 -

Fatal error: Unexpectedly found nil while unwrapping an Optional value

   let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
     let secondViewController = storyBoard.instantiateViewController(withIdentifier: "taskListController") as! TaskListController
     self.navigationController!.pushViewController(secondViewController, animated: true)

请让我知道我的错误。 enter image description here

最佳答案

在推送 VC 之前,必须先添加导航 Controller

使用代码的导航 Controller

func goToMyVC(){
let storyboard = UIStoryboard(name: "Main", bundle: nil)
        if let mainView = storyboard.instantiateViewController(withIdentifier: "yourVC") as? yourVC
        {
            self.window = UIWindow(frame: UIScreen.main.bounds)
            let nav1 = UINavigationController()
            nav1.isNavigationBarHidden = true //Show or hide nav bar
            nav1.viewControllers = [mainView]
            self.window!.switchRootViewController(nav1)
            self.window?.makeKeyAndVisible()
        } }

在你的 View Controller 中调用这个函数,

let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.goToMyVC()

故事中的导航 Controller

let vc = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "controller") as? controller

self.navigationController?.pushViewController(vc!, animated: true)

关于ios - 推送导航 Controller 时出现异常 - iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57218563/

相关文章:

ios - SceneKit 不会在 Xcode 12 中填满屏幕

ios - 多个纬度经度之间的Google Map绘制折线

iOS 框架 : what is ABT (ABTViewSelectorComponent, ABTWeakValue、ABTDestructonomicon 等)?

ios - 使用 AFNetworking 2.0 上传图像

Iphonesdk合并三张图片两张单图

ios - 我应该将什么传递给 `Result<Void, Error>` 的 .success() ?

swiftui - 检查 pickerView SwiftUi 中的选择

objective-c - 空白项目中的编译器错误

php - 使用 PHP 或 Javascript 检查 iOS 版本是否为 3.0 或更高版本

ios - 导入唯一对象时 CoreStore 映射关系