Gold Challenge - App crashes when I tap Change Date

I created the segue from the Change Date button to my new DateUpdateViewController:

And I created DateUpdateViewController similar to DetailViewController:

import UIKit

class DateUpdateViewController: UIViewController {

@IBOutlet var datePicker: UIDatePicker!

var item: Item!

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    
    // "Save change to item"
    item.dateCreated = datePicker.date
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    datePicker.date = item.dateCreated // Gold Challenge - default to item's current dateCreated value

}

}

When I tap Change Date, Xcode gives me this error:

LootLogger[94473:8826416] [Storyboard] Unknown class DateUpdateView in Interface Builder file.
Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value: file DateUpdateViewController.swift, line 26

referring to

datePicker.date = item.dateCreated

from above.

I tried the challenge with this addition in SceneDelegate.swift:

// Gold Challenge - add entry for new DateUpdateViewController
    let dateController = navController.topViewController as! DateUpdateViewController

But the app doesn’t even launch:

Could not cast value of type 'LootLogger.ItemsViewController' (0x101564d58) to 'LootLogger.DateUpdateViewController' (0x101564cb8).

Was there something I overlooked?

My first guess would be that you forgot to set dateUpdateViewController.item when the segue is triggered. You could verify that by setting a breakpoint on the line that’s crashing, and checking to see if item is nil.

This doesn’t work because when the program first starts running dateController isn’t the top view controller of navController, itemViewController is. I didn’t find it necessary for SceneDelegate to obtain a reference to DateUpdateViewController.

Ah…

I added this to DetailViewController.swift:

 // for Gold Challenge
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
       // If the triggered segue is the "showItem" segue
       switch segue.identifier {
       case "showDate":
        let dateUpdateViewController = segue.destination as! DateUpdateViewController
        dateUpdateViewController.item = item
           
       default:
           preconditionFailure("Unexpected segue identifier.")
       }
   }

I can now reach the new page with my DatePicker, but the console still logs this:

LootLogger[96030:8863566] [Storyboard] Unknown class DateUpdateView in Interface Builder file.

Have you checked to make sure that the segue identifier in the story board is the same as the one in your code? I chased my tail for a day until I finally figured that one out.

I use simple way adding UIDatePicker in storyboard.
I try to creat UIDatePicker using code. It is complicated.
It’s tricky way show and hide UIDatePicker.

@IBOutlet weak var datePicker: UIDatePicker!

override func viewDidLoad() {
    super.viewDidLoad()
    datePicker.isHidden = true

}
@IBAction func changeDate(_ sender: UIDatePicker) {
    print(sender.date)
  
    let formatter = DateFormatter()
    formatter.dateFormat = "MM.dd.yyyy"
    dateLabel.text = formatter.string(from: datePicker.date)
    item.dateCreated = sender.date
    datePicker.isHidden = true

}

Yup - segue name matches between storyboard and code (‘showDate’).