Swift自定义注释无法选择

时间:2017-01-02 16:28:13

标签: swift xcode

我在地图上有自定义annotationView,当我选择自定义注释时,我想创建一个动作并将注释数据传递给另一个视图。它工作的最佳方式我必须使用内置函数didSelect。当我选择自定义注释时,没有任何事情发生。我没有说明为什么didSelect函数没有得到提前感谢你。

   class RquestCustomPointAnnotation:MKPointAnnotation {
    var image: String!
}

func placeRquestByUsersOnMap(){
        //retrieve item from firebase
        var markerArray = [MKPointAnnotation]()
        let path = "rquest/frontEnd/posts/userCreatedPost"

        self.childRef(path).observe(.childAdded, andPreviousSiblingKeyWith: {snapshot, _ in
            //get status
            if let status = snapshot.childSnapshot(forPath: "status").value as? String {
                if status == "pending"{
                    let indentifier = "rquest"
                    if let coordinate = snapshot.childSnapshot(forPath: "coordinate").value as? NSDictionary {
                        let lat = coordinate["lat"] as! Double
                        let long = coordinate["long"] as! Double
                        let location = CLLocationCoordinate2D(latitude: lat, longitude: long)
                        let point = RquestCustomPointAnnotation()
                        let rquestView = MKAnnotationView(annotation: point, reuseIdentifier: indentifier)
                        point.image = "22"

                        let key = snapshot.key
                        point.coordinate = location
                        point.accessibilityValue = key
                        point.accessibilityLabel = "Rquest"
                        markerArray.append(point)

                        self.mapView.addAnnotation(rquestView.annotation!)

                        //create an obserarver to check if it is
                        let paths = "rquest/frontEnd/posts/userCreatedPost/\(key)/"
                        self.childRef(paths).observe(.childChanged, andPreviousSiblingKeyWith: { (snap, _) in
                            if snap.key == "status" {
                                if let status = snap.value as? String {
                                    if status != "pending" {
                                        for i in markerArray {
                                            if i.accessibilityValue == key {
                                                self.mapView.removeAnnotation(i)
                                            }
                                        }
                                    }
                                }
                            }
                        })
                    }
                }
            }
        })
    }

 func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
 if annotation is RquestCustomPointAnnotation {
            let reuseIdentifier = "rquest"
            var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseIdentifier)

            if annotationView == nil {
                annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseIdentifier)
                annotationView?.canShowCallout = true
            } else {
                annotationView?.annotation = annotation
            }

            let customPointAnnotation = annotation as! RquestCustomPointAnnotation
            annotationView?.image = UIImage(named: customPointAnnotation.image)


            return annotationView
        }

}


 func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {

     print("did select")

    }

1 个答案:

答案 0 :(得分:1)

您已将canShowCallout设置为true。那么,当你点击它时,你想要标注还是打电话给didSelect? (通常你会做一个或另一个,但不能同时做两个。)你看到你的标注了吗?

我注意到一种奇怪的行为:如果(a)注释没有title,并且(b)注释视图的canShowCallouttrue ,那么它不仅不能显示标注,而且还会阻止调用didSelect

您可能希望将canShowCallout转为false,或确保您的注释具有title