When trying to segue to a view controller from a table view i get this error: Unexpectedly found nil while unwrapping

When trying to segue to a view controller from a table view i get this error: Unexpectedly found nil while unwrapping

我有一个名为 "hydrogenSegue" 的转场,从 "hydrogenBoxButton" 到 "Hydrogen" 视图控制器。但是,我还想实现一个 table 视图,这样我就可以搜索一个元素。我试图编写代码,以便在单击单元格时它会转到元素的视图。我这里以氢为例。

在我的 ViewController.swift 主文件中,我用这个来传输数据:

override 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


    }
}

在 hydrogenViewController.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)"

}

}

我在以下位置收到错误消息:

let hydrogenAtomicNumberString = String("\(hydrogenAtomicNumberPassed!)")

我假设如果我只修复那一行,它也会发生在这条线上:

let hydrogenAtomicMassString = String("\(hydrogenAtomicMassPassed!)")

我的 "searchViewController"(用于 table 视图的 .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)
     }

}

当我单击 table 视图中的 "Hydrogen" 单元格时,它崩溃并出现此错误:

Hydrogen cell

The crash

当我单击此图像中的 "H" 按钮时,它将带我到氢视图控制器:

Image of the Hydrogen Button in the simulator (Top Left)

Image of the Hydrogen View Controller

我希望氢电池像按钮一样连接到氢视图控制器。

当早些时候出现同样的问题时,我只是对情节提要中的 segue 名称有疑问。但是,由于 table 视图中没有可见的 segue,我不知道如何解决这个问题。

我试过这个:

        performSegue(withIdentifier: "hydrogenSegue", sender: nil)

我在想我可以重用从按钮到视图控制器的 "hydrogenSegue" 但我收到了 SIGABRT 错误。它只是说没有名称为 "hydrogenSegue." 的 segue 如果我能以某种方式重用该 segue 最好,因为一切都已经连接但我现在发现 "searchViewController" 不能识别segue。感谢任何帮助,我的主要目标是让被点击的单元格移动到元素的指定视图。我尝试提供尽可能多的信息,但不会太长,如果需要更多信息,我应该能够提供。

嗯。第一个回答

在你的 hydrogenViewController 中试试这行。

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

现在,segues 仅在几个 viewController 之间 "lives",如果你有第三个视图控制器,最后一个将无法识别他。

另外,您正在使用 segues 和导航控制器,从我的角度来看,将两者混合是一个坏主意,我的意思是,有些特定的应用程序可以使用这两种方式来呈现视图,这只是一个建议。

如果你想通过 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)
}

如有疑问,请告诉我,我会尽力帮助你。