How can I perform an action when I click on the output from MapKit? ios9, swift 2

I created a card with tons of pins all over it, and clicking on a pin pops up a default “bubble” this time.

What I really would like to do is not to pop up the bubble at all, but to call another function instead. All my searches have led people who want to create new user annotations with different views, etc., I just want to call the function, and I'm not sure where I would try to call it. I am new to ios development and it seems like this should be pretty simple, but I find that this is usually not the case.

+4
source share
2 answers

-, , UX , , , , , / (, / ), , . . .

Golden Gate Bridge

- , , , () ; (b) MKMapViewDelegate didSelectAnnotationView , , .


, delegate , - Swift 3/4:

private let reuseIdentifier = "MyIdentifier"

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    if annotation is MKUserLocation { return nil }

    var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseIdentifier) as? MKPinAnnotationView
    if annotationView == nil {
        annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseIdentifier)
        annotationView?.tintColor = .green                // do whatever customization you want
        annotationView?.canShowCallout = false            // but turn off callout
    } else {
        annotationView?.annotation = annotation
    }

    return annotationView
}

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
    // do something
}

Swift 2:

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
    if annotation is MKUserLocation { return nil }

    var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseIdentifier) as? MKPinAnnotationView
    if annotationView == nil {
        annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseIdentifier)
        annotationView?.tintColor = UIColor.greenColor()  // do whatever customization you want
        annotationView?.canShowCallout = false            // but turn off callout
    } else {
        annotationView?.annotation = annotation
    }

    return annotationView
}

func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
    // do something
}
+3

, canShowCallout MKAnnotationView.

anView!.canShowCallout = false

didSelectAnnotationView MapView - pin.you didSelectAnnotationView.

func mapView(mapView: MKMapView!, didSelectAnnotationView view: MKAnnotationView!)
{
    //Pin clicked, do your stuff here
}
+2

All Articles