"Unexpectedly found nil while implicitly unwrapping an Optional value" using the Date() structure, then transitioning to another storyboard

30 views Asked by At

I created a label to show the current date when the view loads, but when I transition to another view, I get the: Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value.

The UILAbel works fine and shows the current date, but when I press the button to go to the other View, then it crashes giving the error message at the:

theDate.text = formatter.string(from: date)

The button also works fine if I remove the code in the viewDidLoad

I did check the connections they are linked.

I am very new to swift, been stuck for a while now trying to solve this.

class ViewController: UIViewController {

    @IBOutlet weak var theDate: UILabel!
    @IBAction func but_test(_ sender: Any) {
        
      let testing = storyboard!.instantiateViewController(withIdentifier: "blue_vc")
      present(testing, animated: true, completion: nil)
        
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        let date = Date()
        let formatter = DateFormatter()
        formatter.timeZone = .current
        formatter.dateStyle = .medium
        formatter.locale = .current
        theDate.text = formatter.string(from: date) //I get the error message here. 
        
    }

}
0

There are 0 answers