使用rightCalloutAccessoryView根据注释选择不同的VC

时间:2018-11-06 16:07:33

标签: swift mapkit mapkitannotation

我的地图中有几个不同的注释,用于存储不同的信息。  现在,annotationView的标注btn可以按我的要求工作。  我正在尝试在anView中添加一个执行不同segue的按钮

    //MARK: - Custom Annotation
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    guard !(annotation is MKUserLocation) else {
        return nil
    }

        if !(annotation is CatchAnnotation) {


            let annotationView = MKPinAnnotationView(annotation:annotation, reuseIdentifier:"")
            annotationView.isEnabled = true
            annotationView.canShowCallout = true
            annotationView.animatesDrop = true

            let btn = UIButton(type: .detailDisclosure)
            annotationView.rightCalloutAccessoryView = btn

            if (annotation.title! == "Start") {
                annotationView.image = UIImage(named: "startPin")
                btn.isHidden = true
            }
            else if (annotation.title! == "The End") {
                annotationView.image = UIImage(named: "stopPin")
                btn.isHidden = true
            }

            return annotationView

//                return nil
        }

        let reuseID = "pin"

        var anView = mapView.dequeueReusableAnnotationView(withIdentifier: "pin")
        if anView == nil {
            anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseID)
            anView!.canShowCallout = true


            let btn2 = UIButton(type: .detailDisclosure)
            anView!.rightCalloutAccessoryView = btn2


        }
        else {
            anView!.annotation = annotation
        }

        let cpa = annotation as! CatchAnnotation
        anView!.image = UIImage(named:cpa.pinImageName)

        return anView
    }


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

        performSegue(withIdentifier: "editInfo", sender: view)
    }
}

0 个答案:

没有答案