navigationController!.pushViewController giving Fatal error: Unexpectedly found nil while unwrapping an Optional value:
navigationController!.pushViewController giving Fatal error: Unexpectedly found nil while unwrapping an Optional value:
我正在尝试从一个故事板导航到另一个故事板。这是我的代码 -
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "detail") as! detailsArticle
controller.jsonData = json2["post"]
self.navigationController!.pushViewController(controller, animated: true)
但是,当我在单击运行上述代码的按钮后尝试导航时,出现异常错误:
致命错误:解包可选值时意外发现 nil:文件
我该如何解决?
有 2 个地方可能会导致崩溃。
detailsArticle
的标识符未设置为“详细信息”。
- 您尝试推送的控制器未嵌入
navigationController
。
尝试 运行 并检查控制台以查看我添加的调试消息:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
guard let controller = storyboard.instantiateViewController(withIdentifier: "detail") as? detailsArticle else { print("detail vc id not set"); return }
controller.jsonData = json2["post"]
guard let navigationController = navigationController else { print("this vc is not embedded in navigationController"); return }
navigationController.pushViewController(controller, animated: true)
从评论来看,这是导致问题的第二个问题。要在此处修复它,请执行以下步骤:
- 转到
<StoryboardName>.storyboard
当前UIViewController
子class所在的位置和select当前UIViewController
场景
- 从
Menu bar
选择 Editor
和 Embedd In
和 select Navigation Controller
。
我正在尝试从一个故事板导航到另一个故事板。这是我的代码 -
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "detail") as! detailsArticle
controller.jsonData = json2["post"]
self.navigationController!.pushViewController(controller, animated: true)
但是,当我在单击运行上述代码的按钮后尝试导航时,出现异常错误:
致命错误:解包可选值时意外发现 nil:文件
我该如何解决?
有 2 个地方可能会导致崩溃。
detailsArticle
的标识符未设置为“详细信息”。- 您尝试推送的控制器未嵌入
navigationController
。
尝试 运行 并检查控制台以查看我添加的调试消息:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
guard let controller = storyboard.instantiateViewController(withIdentifier: "detail") as? detailsArticle else { print("detail vc id not set"); return }
controller.jsonData = json2["post"]
guard let navigationController = navigationController else { print("this vc is not embedded in navigationController"); return }
navigationController.pushViewController(controller, animated: true)
从评论来看,这是导致问题的第二个问题。要在此处修复它,请执行以下步骤:
- 转到
<StoryboardName>.storyboard
当前UIViewController
子class所在的位置和select当前UIViewController
场景 - 从
Menu bar
选择Editor
和Embedd In
和 selectNavigation Controller
。