Dimitri T
Dimitri T

Reputation: 951

how to segue from 2nd button on MKannotation?

I have added a left button to my MKAnnotation. I want it to show this as well as the right button and images I have set below. This code below is showing both buttons but only recognizing the right button click to my segue for NewEntry. How can specify the calloutAccessoryControl to recognize the left button tapped, as well as the right button tapped, and add the additional segue from the left button to 'left detail' for my LeftButtonDetailViewController?

Here is my code:

//MARK: - MKMapview Delegate
extension MapViewController: MKMapViewDelegate {

    func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {

        guard let subtitle = annotation.subtitle! else { return nil }

        if (annotation is SpecimenAnnotation) {
            if let annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(subtitle) {
                return annotationView
            } else {
                let currentAnnotation = annotation as! SpecimenAnnotation
                let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: subtitle)

                switch subtitle {
                case "Uncategorized":
                    annotationView.image = UIImage(named: "IconUncategorized")
                case "Documentaries":
                    annotationView.image = UIImage(named: "IconDocumentaries")
                default:
                    annotationView.image = UIImage(named: "IconUncategorized")
                }

                annotationView.enabled = true
                annotationView.canShowCallout = true

                let detailDisclosure1 = UIButton(type: UIButtonType.DetailDisclosure)
                let detailDisclosure2 = UIButton(type: UIButtonType.InfoDark)
                annotationView.rightCalloutAccessoryView = detailDisclosure1
                annotationView.leftCalloutAccessoryView = detailDisclosure2

                if currentAnnotation.title == "Empty" {
                    annotationView.draggable = true
                }

                return annotationView
            }
        }
        return nil
    }

    func mapView(mapView: MKMapView, didAddAnnotationViews views: [MKAnnotationView]) {

        for annotationView in views {
            if (annotationView.annotation is SpecimenAnnotation) {
                annotationView.transform = CGAffineTransformMakeTranslation(0, -500)
                UIView.animateWithDuration(0.5, delay: 0.0, options: .CurveLinear, animations: {
                    annotationView.transform = CGAffineTransformMakeTranslation(0, 0)
                    }, completion: nil)
            }
        }

    }

    func mapView(mapView: MKMapView, annotationView: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
        if let specimenAnnotation =  annotationView.annotation as? SpecimenAnnotation {
            performSegueWithIdentifier("NewEntry", sender: specimenAnnotation)

        }
    }

    func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, didChangeDragState newState: MKAnnotationViewDragState, fromOldState oldState: MKAnnotationViewDragState) {
        if newState == .Ending {
            view.dragState = .None
        }
    }

}

Upvotes: 1

Views: 56

Answers (1)

Rob
Rob

Reputation: 438257

You can compare the accessory to the left and right accessories, to see which was selected:

func mapView(mapView: MKMapView, annotationView: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
    if let specimenAnnotation = annotationView.annotation as? SpecimenAnnotation {
        if control == annotationView.leftCalloutAccessoryView {
            // left
        } else if control == annotationView.rightCalloutAccessoryView {
            // right
        } else {
            fatalError("Unknown accessory")
        }
    }
}

Upvotes: 1

Related Questions