Search code examples
iosswiftmkannotationview

Pass data to MapDetailViewController


I have annotations on my map. I want to open a new view controller when user taps AccessoryControl and pass data to that view controller.

So I do this:

func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
    self.performSegueWithIdentifier("Details", sender: view)
}

then:

if segue.identifier == "Details" {

        let placeDetailViewController = segue.destinationViewController as! PlaceDetailViewController

        let place: Place = places[?index?]
        placeDetailViewController.place = place
}

So I want to get the index of my annotation and don't know how to do this.

Help. Thanks.


Solution

  • You need to do it in prepareForSegue:

    override func prepareForSegue(segue: UIStoryboardSegue?, sender: AnyObject?) {
            if segue!.identifier == "Details" {
                let vc = segue!.destinationViewController as PlaceDetailViewController
                vc.data = "YOUR_DATA_HERE"
            }
        }