viewForAnnotation返回nil

时间:2016-09-04 10:54:57

标签: ios swift mapkit

我正在尝试制作自定义注释。我创建了一个自定义注释类,我设计了注释视图。我试图在我的Viewcontroller中的viewForAnnotation中调用该类。当我运行应用程序时,它将显示基本视图。这是功能。如果您想要查看自定义注释类,请告诉我。

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


    let reuseIdentifier = "CustomAnnotation"


    if (annotation.isKindOfClass(CustomAnnotation)) {
        var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseIdentifier) as! CustomAnnotation!


        annotationView = CustomAnnotation(annotation: annotation, reuseIdentifier: reuseIdentifier)

        return annotationView
    }
        else {
            return nil
    }

}

1 个答案:

答案 0 :(得分:0)

func mapView(mapView:MKMapView,viewForAnnotation annotation:MKAnnotation) - > MKAnnotationView? {         如果注释是MKUserLocation {             返回零         }

    let reuseId = "pin"
    var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
    if pinView == nil {
        pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        pinView?.animatesDrop = true
        pinView?.canShowCallout = false
        pinView?.draggable = true
              }
    else {
    }

    return pinView
}