I am trying to pass the coordinates, title, and subtitle of a selected MKAnnotation from ViewController1 (VC1) to ViewController2 (VC2) when the DetailDisclosure button of rightCalloutAccessoryView is tapped. I have a segue from VC1 to VC2 with the identifier viaSegue. I have a label with identifier viaSegueLabel in VC2 where I want to display the coordinates as a String.
The function that customizes the MKAnnotation's call out so that it displays a DetailDisclosure button in the rightCalloutAccessoryView looks like:
// Customize Annotation Callout
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
// 1
let identifier = "Capital"
// 2
if annotation.isKindOfClass(Capital.self) {
// 3
var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier)
if annotationView == nil {
//4
annotationView = MKPinAnnotationView(annotation:annotation, reuseIdentifier:identifier)
annotationView!.canShowCallout = true
// 5
let btn = UIButton(type: .DetailDisclosure)
annotationView!.rightCalloutAccessoryView = btn
} else {
// 6
annotationView!.annotation = annotation
}
return annotationView
}
// 7
return nil
}
The function that takes the user from VC1 to VC2 when the DetailDisclosure button is tapped looks like:
// When righCalloutAccessoryView is tapped, segue to newView
func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
self.performSegueWithIdentifier("newView", sender: view)
}
And the function I think I need to implement to accomplish this looks like:
// Pass data to newView
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if (segue.identifier == "newView") {
let destViewController:BusStopSettingsViewController = segue.destinationViewController as! BusStopSettingsViewController
destViewController.viaSegue = // not sure how to reference selected Annotation here
}
}
In the last line of prepareForSegue(), I need to reference the MKAnnotation that is currently selected. Is there a method built into Swift that lets me do this, or should I make the Annotation global?