-1

Swift-Newbie question:

I have two view controllers: My home VC which uses MapKit and a second 'MapDetailViewController' to display additional information.

On the home VC, I have added a point of interest with an annotation to display the name of the point and subtitle when the user clicks on the pin. How can I segue to my MapDetailViewController when the user clicks on the map annotation?

// Map view delegate which handles the annotation view
    extension ViewController: MKMapViewDelegate {
    func mapView(_ mapView: MKMapView, viewFor annotation: 
        MKAnnotation) -> 

MKAnnotationView? {

    guard let annotation = annotation as? Artwork else { return nil }

    let identifier = "marker"

    var view: MKMarkerAnnotationView

    if let dequeuedView = 
mapView.dequeueReusableAnnotationView(withIdentifier: identifier) as? 
MKMarkerAnnotationView {

        dequeuedView.annotation = annotation

        view = dequeuedView

    } else {

        view = MKMarkerAnnotationView(annotation: annotation, 
reuseIdentifier: identifier)

        view.canShowCallout = true

        view.calloutOffset = CGPoint(x: -5, y: 5)

        // Broke up the button initialisation so that it has unique id 'button'
        let button = UIButton(type: .detailDisclosure)

        view.rightCalloutAccessoryView = button

        // Make the segue happen - tried to make it happen when object 'button' is pressed

        performSegue(withIdentifier: "MapDetail", sender: button)

    }

    return view
}
Kris
  • 11
  • 1

1 Answers1

0

This is an extract from an old project I was working on. It opens a new tableview controller when the information accessory is tapped on the annotation. I had a made a custom class called Annotation which held all the information about the annotation. Im not sure if this will help :)

func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView,
            calloutAccessoryControlTapped control: UIControl) {

        let annotation = view.annotation as! Annotation
        let event = annotation.event

        if control == view.rightCalloutAccessoryView {
            let destination = self.storyboard!.instantiateViewController(withIdentifier: "EventViewController") as! EventViewController
            self.navigationController!.pushViewController(destination, animated: true)
            destination.event = event
    }
}  
ozzyzig
  • 709
  • 8
  • 19