[英]When trying to segue to a view controller from a table view i get this error: Unexpectedly found nil while unwrapping
I have a segue named "hydrogenSegue" from a "hydrogenBoxButton" to a "Hydrogen" view controller.我有一个名为“hydrogenSegue”的segue,从“hydrogenBoxButton”到“Hydrogen”视图控制器。 However, I also wanted to implement a table view so I could search for an element.
但是,我还想实现一个表格视图,以便我可以搜索一个元素。 I tried to make the code so when the cell is clicked it will segue over to the element's view.
我尝试编写代码,以便在单击单元格时它会转到元素的视图。 I used hydrogen as an example here.
我在这里以氢气为例。
In my main ViewController.swift file, I have this to transfer the data:在我的主 ViewController.swift 文件中,我有这个来传输数据:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {覆盖 func prepare(for segue: UIStoryboardSegue, sender: Any?) {
//If identifier equals the hydrogen element go to the elements Swift file
if segue.identifier == "hydrogenSegue" {
let hydrogenAtomicNumberPassing = segue.destination as! hydrogenViewController
hydrogenAtomicNumberPassing.hydrogenAtomicNumberPassed = hydrogenAtomicNumber
let hydrogenAtomicMassPassing = segue.destination as! hydrogenViewController
hydrogenAtomicMassPassing.hydrogenAtomicMassPassed = hydrogenAtomicMass
}
}
In the hydrogenViewController.swift file I have this:在 HydroViewController.swift 文件中,我有这个:
import UIKit
class hydrogenViewController: UIViewController {
var hydrogenAtomicNumberPassed: Int!
var hydrogenAtomicMassPassed: Float!
@IBOutlet weak var hydrogenInformationLabel: UILabel!
@IBOutlet weak var hydrogenAtomicNumberLabel: UILabel!
@IBOutlet weak var hydrogenAtomicMassLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
//Setting the background color
self.view.backgroundColor = UIColor.gray
//Converting hydrogen's atomic number from an Int to a String
let hydrogenAtomicNumberString = String("\(hydrogenAtomicNumberPassed!)")
hydrogenAtomicNumberLabel.text = "Atomic Number: \(hydrogenAtomicNumberString)"
//Converting hydrogen's atomic mass from a Float to a String
let hydrogenAtomicMassString = String("\(hydrogenAtomicMassPassed!)")
hydrogenAtomicMassLabel.text = "Atomic Mass: \(hydrogenAtomicMassString)"
}
}
I am getting the error at:我在以下位置收到错误:
let hydrogenAtomicNumberString = String("\(hydrogenAtomicNumberPassed!)")
I'm assuming it would happen to this line also if I fix only that line:我假设如果我只修复那条线,它也会发生在这条线上:
let hydrogenAtomicMassString = String("\(hydrogenAtomicMassPassed!)")
I have this code in my "searchViewController" (the .swift file used for the table view):我的“searchViewController”中有这个代码(用于表格视图的 .swift 文件):
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("row selected : \(indexPath.row)")
if indexPath.row == 0 {
let hydrogenSearchSegue = UIStoryboard(name:"Main",
bundle:nil).instantiateViewController(withIdentifier: "hydrogenView") as!
hydrogenViewController
self.navigationController?.pushViewController(hydrogenSearchSegue,
animated:true)
}
}
When I click on the "Hydrogen" cell in the table view it crashes to this error:当我点击表格视图中的“Hydrogen”单元格时,它会崩溃到这个错误:
When I click on the "H" button in this image it will take me to the hydrogen view controller:当我单击此图像中的“H”按钮时,它会将我带到氢视图控制器:
Image of the Hydrogen Button in the simulator (Top Left)模拟器中的氢按钮图像(左上角)
Image of the Hydrogen View Controller Hydrogen 视图控制器的图像
I want the hydrogen cell to segue over to the hydrogen view controller just like the button can.我希望氢电池就像按钮一样转到氢视图控制器。
When this same issue came up earlier I just had an issue with the name of the segue in the storyboard.早些时候出现同样的问题时,我只是对故事板中的 segue 名称有疑问。 However, because there is no visible segue from the table view, I don't know how to fix the issue.
但是,由于表视图中没有可见的 segue,我不知道如何解决该问题。
I've tried this:我试过这个:
performSegue(withIdentifier: "hydrogenSegue", sender: nil)
I was thinking that I could just reuse the "hydrogenSegue" from the button to the view controller but I get a SIGABRT error.我在想我可以重用从按钮到视图控制器的“hydrogenSegue”,但我收到了一个 SIGABRT 错误。 It just says that there is no segue with the name "hydrogenSegue."
它只是说没有名为“hydrogenSegue”的segue。 It would be best if I could just reuse that segue in a way because everything is already connected but I now found out that the "searchViewController" can't recognize the segue.
最好我能以某种方式重用该 segue,因为一切都已经连接,但我现在发现“searchViewController”无法识别 segue。 Any help is appreciated and my main goal is to just get the cell that is clicked on to move over to the element's designated view.
任何帮助表示赞赏,我的主要目标是让点击的单元格移动到元素的指定视图。 I tried to provide as much information as possible without making it to long and if there is any more information needed, I should be able to provide it.
我试图提供尽可能多的信息,而不会太长,如果需要更多信息,我应该能够提供。
well.好。 first answer
第一个回答
in your hydrogenViewController try with this lines.在您的 hydroViewController 中尝试使用此行。
var hydrogenAtomicNumberPassed: Int?
var hydrogenAtomicMassPassed: Float?
override func viewDidLoad(){
super.viewDidLoad()
self.viewBackgroundColor = .gray
}
override func viewWillAppear(){
super.viewWillAppear()
if let number = hydrogenAtomicNumberPassed
{
hydrogenAtomicNumberLabel.text = "Atomic Number: \(number)"
}
if let mass = hydrogenAtomicMassPassed
{
hydrogenAtomicMassLabel.text = "Atomic Mass: \(mass)"
}
}
Now, the segues only "lives" between a couple viewControllers, if you have a third view controller, the last will not recognize him.现在,segues 只在几个 viewController 之间“存活”,如果你有第三个视图控制器,最后一个将无法识别他。
other thing, you are using segues and navigation controller, from my point of view, it's a bad idea mixed both, I mean, there are specific apps that can use both ways to present views, only is a advice.另一件事,你正在使用 segues 和导航控制器,从我的角度来看,混合两者是一个坏主意,我的意思是,有特定的应用程序可以使用这两种方式来呈现视图,只是一个建议。
if you want to pass data with pushviewcontroller only use this line如果你想用 pushviewcontroller 传递数据只使用这一行
if indexPath.row == 0 {
let hydrogenSearchSegue = UIStoryboard(name:"Main",bundle:nil).instantiateViewController(withIdentifier: "hydrogenView") as! hydrogenViewController
hydrogenSearchSegue.VAR_hydrogenViewController = YOURVAR_INYOURFILE
self.navigationController?.pushViewController(hydrogenSearchSegue, animated:true)
}
tell me if you have doubts, and I will try to help you.如果您有疑问,请告诉我,我会尽力帮助您。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.