I have 3 VC - VC1, VC2 & VC3
I'm creating an unwind segue where -
- VC1 is destination
- VC2 is source
So, I've add Marker function in VC1 -
@IBAction func unwindToHomeViewController(_ sender: UIStoryboardSegue) {}
and in VC2 I've created two variable -
var userSelectedPlacesLatitude: Double = 0
var userSelectedPlacesLongitude: Double = 0
which will be updated in tableView -
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.userSelectedPlacesLatitude = suggestedPlacenames[indexPath.row].geometry.coordinates[1]
self.userSelectedPlacesLongitude = suggestedPlacenames[indexPath.row].geometry.coordinates[0]
print("In didSelectRowAt", userSelectedPlacesLatitude, userSelectedPlacesLongitude)
}
and then prepare for segue -
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let destinationVC = segue.destination as! VC1
print("In Segue preperation",userSelectedPlacesLatitude, userSelectedPlacesLongitude)
destinationVC.userSelectedPlacesLatitude = self.userSelectedPlacesLatitude
destinationVC.userSelectedPlacesLongitude = self.userSelectedPlacesLongitude
destinationVC.reloadWeatherDataStatus = true
}
But from print value I'm seeing that prepareforSegue is called earlier than didSelectRowAt. Hence I'm not getting expected value in prepareforsugue
In Segue preperation 0.0 0.0
In didSelectRowAt 49.3227937844972 31.3202829593814
Hence 0.0 0.0 is passing all the time to VC1. How can I fix this problem?
The problem you are experiencing results from having at the unwind segue linked directly from the table view cell in your storyboard. As soon as the user taps the row, the unwind segue fires. The
didSelectRow(at:)function is called after, but this is too late; You are already back in VC1.While you can use
prepareForSegueto send data to VC1, a better approach is to use thesenderpassed tounwindToHomeViewControllerto let VC1 get the data from VC2.This means that VC2 doesn't need to know anything about VC1. You can also get rid of the
reloadWeatherDataStatusproperty and simply reload the weather data status in the unwind function.You should:
unwindToHomeViewControllerunwindToVC1Remove
prepare(for segue: sender:)from VC2In VC1