Gold challenge - using done button to dismiss datePicker

I have been working on the gold challenge for several days now and cannot seem to get myself unstuck. I can’t seem to make the datePicker go away once I’m done. I tried a couple of different ways to add a done button and now it appears but clicking it does nothing. I feel like my code is pretty messy now since I have tried so many different things but here is the relevant part:

@IBAction func changeDate(_ sender: Any) {

datePicker.frame = CGRect(x: 10, y: 55, width: self.view.frame.width, height: 200)
datePicker.timeZone = NSTimeZone.local
datePicker.backgroundColor = UIColor.white
datePicker.datePickerMode = UIDatePicker.Mode.date
datePicker.addTarget(self, action: #selector(DetailViewController.datePickerValueChanged(_:)), for: .valueChanged)

    self.view.addSubview(datePicker)
    
    self.dateField.inputView = datePicker
   
    
    let toolbar = UIToolbar (frame: CGRect(x: 0, y: 10, width: datePicker.frame.size.width, height: 44))
    toolbar.sizeToFit()
         
    let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(doneDatePickerPressed(sender:)))
          toolbar.barStyle = .default
         toolbar.setItems([doneButton], animated:true)
   self.dateField.inputView = toolbar
   self.datePicker.addSubview(toolbar)
       }
@objc func doneDatePickerPressed(sender: UIBarButtonItem){
  
    self.dateField.resignFirstResponder()
}

Does anyone have any clues as to how to get datePicker to go away?

Don’t despair. Coding was not meant to be pain free. You just have to try harder :slight_smile:

Did you check to see if that function is being called at all?

@objc func doneDatePickerPressed (sender: UIBarButtonItem){
    print ("\(#function):...")
    self.dateField.resignFirstResponder ()
}

Did you connect the function to the action outlet of the Done button?

@IBAction func doneDatePickerPressed (sender: UIBarButtonItem){
    print ("\(#function): ...")
    self.dateField.resignFirstResponder ()
}