Search code examples
iosswiftuibuttonsegueuistoryboardsegue

Segue doesn't work on Map Annotation Button- Can't see what is wrong


I created this app which has fixed annotations on a map and you can press the button to see additional information of the place like a Picture, a Title and some text. The problem is that the button does absolutely nothing, even though I implement the prepareforsegue method.

This is the part of the code that has the issue, also here is a link of the entire project in case the sample code is inadequate Map Project

 func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
    if let annotation = annotation as? Shops{
        let identifier = "pin"
        var view: MKPinAnnotationView
        if let dequeuedView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier)
            as? MKPinAnnotationView {
                dequeuedView.annotation = annotation
                view = dequeuedView
        } else {

            view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
            view.canShowCallout = true
            view.calloutOffset = CGPoint(x: -5, y: 5)

            view.rightCalloutAccessoryView = UIButton(type: .DetailDisclosure) as UIView
        }
        return view
    }
    return nil
}

//Pressing Button and segue

    func Button(mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl){
        if control == view.rightCalloutAccessoryView {

        self.performSegueWithIdentifier("ShowDetails", sender: self)
        }
    }

//that goes to the top override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?){


Solution

  • The MKMapViewDelegate function does not have that signature.

    The correct one is

    func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
        if control == view.rightCalloutAccessoryView {
    
        self.performSegueWithIdentifier("ShowDetails", sender: self)
        }
    }
    

    You started the function with Button instead of mapView