如何从MKannotation上的第二个按钮中删除?

时间:2016-07-25 03:58:31

标签: swift mkannotation

我在MKAnnotation添加了一个左键。我想要它显示这个以及我在下面设置的右键和图像。下面的代码显示了两个按钮,但只识别右键单击我的NewEntry的segue。如何指定calloutAccessoryControl识别点击的左键,以及点击的右键,并将左侧按钮中的附加列号添加到“左侧详细信息”中。我的LeftButtonDetailViewController

这是我的代码:

//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
        }
    }

}

1 个答案:

答案 0 :(得分:1)

您可以将配件与左右配件进行比较,以查看选择的配件:

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")
        }
    }
}