2017-01-02 44 views
1

我在我的地图上有一个自定义annotationView,当我选择自定义注释时,我想创建一个动作并将注释数据传递给另一个视图。它的最佳工作方式我将不得不使用内置的func didSelect。当我选择自定义注释时,什么都不会发生。我没有说明为什么didSelect函数没有被预先称为谢谢。Swift自定义注释没有得到选择

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

    } 
+0

每当我不使用自定义注释它的工作,但是当我使用自定义注释它不起作用。我可以添加一个目标到自定义注释,但我需要使用注释数据,所以addTarget不会好。 – pprevalon

+0

@Rob我添加了更多的代码来帮助解决它 – pprevalon

+0

@Rob通过向注释添加标题来激活'didSelect'函数。我将从'placeRquestByUsersOnMap'删除额外的注释谢谢你的帮助。如果您提交答案,我可以接受它,以便您可以获得积分。 – pprevalon

回答

1

你必须canShowCallout设置为true。所以,当你点击它时,你想要标注还是要拨打didSelect? (通常你会做一个或另一个,但不是两个。)你看到你的标注吗?

我注意到一个奇怪的行为,如果(a)该批注没有一个title,和(b)注解视图的canShowCallouttrue,那么它不仅可以不显示标注,但它也可以防止didSelect从被叫。

您既可以想转canShowCalloutfalse,或请确保您的注释有title