0

I am currently working on a function where the user can tap on a maps annotation and be taken to another screen which will hold an image of that location. I am using Mapbox and the tapOnCallout function to segue to another view controller (imageLocationViewController) which will hold an image view.

The issue I am facing is letting this imageLocationViewController know which annotation has been clicked and therefore which image should be shown. I am using firebase to hold my data and within the annotation data I have a photoUrl which holds the relevant image.

I am currently able to print the relevant name of the annotation and photo Url when I am segueing. However, once I am within the imageLocation View Controller I believe I lose this data?

Does anybody know how I can pass this data into the new view controller so that I can pass the correct photoUrl into the image view.

This is my current code to segue to the imageLocationViewController.

 func mapView(_ mapView: MGLMapView, tapOnCalloutFor annotation: MGLAnnotation) {



    if let annotation = annotation as? SkateAnnotation {



        self.performSegue(withIdentifier: "SkateImageSegue", sender: annotation.id)

        print("YourAnnotation: \(annotation.photoUrl)")

    }
}

Update ** Code for prepare for segue

 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "EditSaveSpotSegue" {
        let destination = segue.destination as! EditSaveSpotViewController
        destination.parkId = sender as! String
    }
}
Cal
  • 61
  • 8
  • Possible duplicate of [Passing Data between View Controllers](https://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – Chris Jun 04 '17 at 18:08
  • My issue is my code already contains override func prepare(for segue: UIStoryboardSegue, sender: Any?) – Cal Jun 04 '17 at 18:21
  • Could you show that code Cal? – Chris Jun 04 '17 at 18:21
  • Hey chris, I have updated the code. This segue takes me to a pop over where I edit the annotation information – Cal Jun 04 '17 at 19:01

1 Answers1

0

I would set a local variable whenever your mapView delegate function fires, just before you call performSegue, and then use that variable in prepareForSegue. See below

 var annotationId: String = ""

 func mapView(_ mapView: MGLMapView, tapOnCalloutFor annotation: MGLAnnotation) {



    if let annotation = annotation as? SkateAnnotation {


        annotationId = annotation.id
        self.performSegue(withIdentifier: "SkateImageSegue", sender:self)

        print("YourAnnotation: \(annotation.photoUrl)")

    }
}


 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "EditSaveSpotSegue" {
        let destination = segue.destination as! EditSaveSpotViewController
        destination.parkId = annotationId
    }
}
Chris
  • 7,830
  • 6
  • 38
  • 72