问题描述
我的故事板上有3个场景.我最初的View Controller是导航控制器,然后是根视图控制器与UI ViewController(视图控制器a)的关系,然后我将按钮从ViewController中的按钮推到了第三个ViewController(视图控制器b)中场景.我给了推送搜索一个标识符.现在,我正在尝试像下面这样在第二个视图控制器(视图控制器a)中准备segue:
I have 3 scenes in my storyboard. My initial View Controller is a Navigation Controller, then there is a relationship root view controller to a UI ViewController (view controller a) and then I have a push segue from a button in the ViewController to the third ViewController (view controller b) in the scene. I have given the push segue an identifier. Now I am trying to prepare my segue in the 2nd view controller (view controller a) like so:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "HistorySegue" {
if let viewController = segue.destination as? HistoryController {
viewController.detailItem = barcodeInt as AnyObject
}
}
}
但是,当我运行此代码并按下控制器a中的按钮时,出现以下错误:
However when I run this code and push the button in controller a I get the following error:
致命错误:尝试桥接一个包含nil的隐式展开的可选
fatal error: attempt to bridge an implicitly unwrapped optional containing nil
我做错了什么?
推荐答案
用以下代码替换代码,至少不会崩溃.
Replace your code with the following, it will not crash at least.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "HistorySegue" {
if let viewController = segue.destination as? HistoryController {
if(barcodeInt != nil){
viewController.detailItem = barcodeInt as AnyObject
}
}
}
}
这篇关于斯威夫特3-准备塞古的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!