0
votes

I currently have a custom MKAnnotationView set in a map callout, and it's working well. I however want to add a button to the callout view, but when i tap the button it closes the annotation view before it gets called. How can i get around this?

Here are pertinent bits of my code: In my view for annotations:

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

    let reuseId = "mapReuseId"
    var mapView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId)
    if mapView == nil {
        mapView = MKAnnotationView(annotation: annotation as! Annotation, reuseIdentifier: reuseId)
        mapView!.canShowCallout = false
    } else {
        mapView!.annotation = annotation as! Annotation
    }
    mapDottView!.image = customImage

    return mapDottView
}

In my didSelect delegate:

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
    let callOut = customCallOutView(with: data)
    view.addSubview(callOut)
    // some layout here
}

The customCallOutView is longish, but the important part is that it has a UIButton which never gets called on tap. Any ideas?

2

2 Answers

0
votes

You can try to put a delay when your button is tapped. You can use Grand Central Dispatch or Perform Selector After Delay.

// Do what you need to do when your button was tapped.
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
  // Dismiss your annotation.
}
0
votes

Try the following code:

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

    let reuseId = "mapReuseId"
    var mapView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId)
    if mapView == nil {
        mapView = MKAnnotationView(annotation: annotation as! Annotation, reuseIdentifier: reuseId)
        mapView!.canShowCallout = true
        let rightButton: AnyObject! = UIButton(type: UIButtonType.detailDisclosure)
        pinView?.rightCalloutAccessoryView = rightButton as? UIView
    } else {
        mapView!.annotation = annotation as! Annotation
    }
    mapDottView!.image = customImage

    return mapDottView
}


func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
    print(#function)
    // this method will be called when the button is tapped.
    // annotation view doesn't disappear
}