Problem:
I'm having trouble setting an instance variable when I return to the main view controller from a secondary view controller.
Setup:
My main view controller has a UIButton associated with a Show segue to a second view controller. This second view controller has a collection view (it's a calendar) that triggers a Show segue to the original view controller. I am trying to set a variable in the first view controller with prepare(for segue:) called in the second view controller. No dice.
Code:
Main View Controller:
class ViewController: UIViewController {
var myDate=Date()
func viewDidAppear(): {
updateDateLabel(myDate)
}
...
}
Calendar (second) View Controller:
class CalendarViewController: UIViewController {
var selectedDate: Date?
func calendar(..., didSelectDate date: Date, ...) {
selectedDate = date
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "dateSelected" {
let vc = segue.destination as! ViewController
vc.myDate = self.selectedDate
}
}
I suspect my main view controller is reinitializing the myDate variable, but how do I address this?