I am trying to pass data over a segue created by tapping an MkAnnotation callout using the calloutAccessoryControlTapped control. I want to go to the EventViewController and make a screen with more information about the MkAnnotation that was selected.
I've tried a number of difference methods including creating a custom class and trying to send that. The segue happens correctly but no data is passed to my second UIViewController (eventViewController).
I assume I am doing the wrong thing every time, but am finding it hard to debug the code due to the point at which I am assigning the data to the variable is also the trigger for the segue.
i.e. I am assuming the data isn't getting assigned at all, but the "selectedAnnotation" variable is getting passed across correctly but obviously its hard to tell.
override func prepare(for segue: UIStoryboardSegue, sender: (Any)?) {
if segue.identifier == "goToEventScreen" {
selectedAnnotation = mapView.selectedAnnotations.lastObject as? MKAnnotation
let destinationVC = segue.destination as! EventViewController
destinationVC.points = selectedAnnotation
}
}
}
'''
extension ViewController: MKMapViewDelegate {
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView,
calloutAccessoryControlTapped control: UIControl){
performSegue(withIdentifier: "goToEventScreen", sender: self)
}
}
Thanks in advance.
You need to set your annotationView
in the delegate method as the sender of performSegue
method.
extension ViewController: MKMapViewDelegate {
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
performSegue(withIdentifier: "goToEventScreen", sender: view)
}
}
Then in prepare for segue method:
override func prepare(for segue: UIStoryboardSegue, sender: (Any)?) {
if segue.identifier == "goToEventScreen" {
if let annotationView = sender as? MKAnnotationView {
selectedAnnotation = annotationView.annotation
let destinationVC = segue.destination as! EventViewController
destinationVC.points = selectedAnnotation
}
}
}
What you do is getting the annotation from the selected annotationView and assigning it to the destinationVC's points.