Search code examples
swiftannotationsseguemapboxcallout

Mapbox - calloutAccessoryControllerTapped


I am having an issue with my calloutAccessoryControllerTapped. At the minute I have two buttons within my callout - each which will segue to a modal pop up on my map view. At the minute my segue works but both buttons segue to the same popup. I need to be able to differentiate between buttons and their segue.

Is this possible? I have googled this question and many answers seem to suggest using this line of code below

if (control == view.leftCalloutAccessoryView) {

However, this is coming up with an error "Value of type 'UIView' has no member 'leftCalloutAccessoryView'. I would really appreciate if somebody could help me with this issue and apologise if I have not clearly explained the issue, I have done so to my best extent.

Below is my code:

func mapView(_ mapView: MGLMapView, annotationCanShowCallout annotation: MGLAnnotation) -> Bool {

    return true


}


func mapView(_ mapView: MGLMapView, annotation: MGLAnnotation, calloutAccessoryControlTapped control: UIControl) {


    self.performSegue(withIdentifier: "Show", sender: view)

}

func mapView(_ mapView: MGLMapView, rightCalloutAccessoryViewFor annotation: MGLAnnotation) -> UIView? {

    guard let skateAnnotation = annotation as? SkateAnnotation else { return nil }

    if skateAnnotation.canEdit {
        return UIButton(type: .detailDisclosure)
    }

    return nil


}

func mapView(_ mapView: MGLMapView, leftCalloutAccessoryViewFor annotation: MGLAnnotation) -> UIView? {

    return UIButton(type: .contactAdd)


}



 func mapView(_ mapView: MGLMapView, imageFor annotation: MGLAnnotation) -> MGLAnnotationImage? {

    return nil

}


}

Solution

  • Something simple (change code for your own requirements as necessary) like this works to distinguish between the left and right callout accessory controls.

    func mapView(_ mapView: MGLMapView, leftCalloutAccessoryViewFor annotation: MGLAnnotation) -> UIView? {
        let button = UIButton(type: .detailDisclosure)
        button.tag = 100
        return button
    }
    
    func mapView(_ mapView: MGLMapView, rightCalloutAccessoryViewFor annotation: MGLAnnotation) -> UIView? {
        let button = UIButton(type: .detailDisclosure)
        button.tag = 101
        return button
    }
    
    func mapView(_ mapView: MGLMapView, annotation: MGLAnnotation, calloutAccessoryControlTapped control: UIControl) {
        // Hide the callout view.
        mapView.deselectAnnotation(annotation, animated: false)
    
        if control.tag == 100 {
            print("left")
        } else if control.tag == 101 {
            print("right")
        }
    }