I try to pass a Date from a ViewController to another one through an unwind segue. The problem is that the data passed is replaced by the result of Date().
Here is the code of the VC where the date is selected :
class DateSelectorViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
...
@IBAction func validateDate(_ sender: CustomButton) {
sendDateToNextVC(sender: sender)
}
func sendDateToNextVC(sender: UIButton) {
let destinationVC = AddActivityViewController()
destinationVC.dateSelected = dateSelected
destinationVC.recurrenceType = selectedFrequency
destinationVC.isRecurrent = recurrent
performSegue(withIdentifier: "sendSelectedDate", sender: sender)
}
}
And here is the code of the VC that receive the date in dateSelected :
class AddActivityViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate {
...
var dateSelected: Date = Date()
...
@IBAction func unwindToAddActiVC(_ sender: UIStoryboardSegue) {
if sender.identifier == "sendSelectedDate" {
setDateLabel()
}
}
func setDateLabel() {
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "fr_FR")
dateFormatter.dateFormat = "dd MMMM yyyy"
dateLabel.text = dateFormatter.string(from: dateSelected)
}
}
I remarked that the line
var dateSelected: Date = Date()
was called multiple times, especially once after that the date was passed from the first VC to the destination VC. I tried using optional but it only leads to obvious errors "found nil while unwrapping".
Please let me know if you need other pieces of code.
Thanks in advance !!