(Swift) PrepareForSegue: fatal error: unexpectedly found nil while unwrapping an Optional value

后端 未结 5 1990
Happy的楠姐
Happy的楠姐 2020-12-16 01:52

DetailViewController:

    @IBOutlet var selectedBundesland: UILabel!

TableViewController:

    override func prepareForSegue         


        
5条回答
  •  有刺的猬
    2020-12-16 02:36

    While the correct solution is to store the text and attach it to the label later in viewDidLoad or something, for testing proposes, you can bypass the issue by forcing the destinationViewController to build itself from storyboard by calling its view property like:

    override func prepare(for segue: UIStoryboardSegue, sender: Any?){
    
         if (segue.identifier == "TestViewController") {
              var vc:FirstViewController = segue.destination as! TestViewController
              print(vc.view)
              vc.testLabel.text = "Hello World!"
         }
    }
    

    made for Swift 3.0 with love

提交回复
热议问题