Problems accessing custom MKAnnotation data via rightCalloutAccessoryView

我是研究僧i 提交于 2019-12-01 13:14:13

问题


I am trying to access some custom data I've setup in my custom annotation data when triggered by the rightCalloutAccessoryView. I am getting a compiler error as described below. Here's my custom MKAnnotation with a couple of extra variables - status & supplierdataindex

class CustomMapPinAnnotation : NSObject, MKAnnotation {
  var coordinate: CLLocationCoordinate2D
  var title: String
  var subtitle: String
  var status: Int             
  var supplierdataindex: Int  

  init(coordinate: CLLocationCoordinate2D, title: String, subtitle: String, status: Int, supplierdataindex: Int) {
    self.coordinate = coordinate
    self.title = title
    self.subtitle = subtitle
    self.status = status
    self.supplierdataindex = supplierdataindex     
  }
}  // CustomMapPinAnnotation

var myCustomMapPinAnnotationArray = [CustomMapPinAnnotation] ()
// I build an array and put that into myCustomMapPinAnnotationArray
...
// I add the annotations initially in viewDidLoad in the ViewController.swift via this call  
theMapView.addAnnotations(myCustomMapPinAnnotationArray)

Everything works great in terms of the map and it's pins but now I want to access the custom parts of that myCustomMapPinAnnotation array, specifically the "status" and and "supplierdataindex" which will drive decisions for more detailed views. I am using the calloutAccessoryControlTapped to catch the click.

The problem is this compiler gives me an error below where I try to setup access that I thought would get me pointed to the custom data that should have been built into the annotation data I thought.

'MKAnnotation" is not convertible to 'CustomMapPinAnnotation'

func mapView(mapView: MKMapView!, annotationView: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {

    if control == annotationView.rightCalloutAccessoryView {
        // This works and prints the data
        println("Right Callout was pressed and title = \(annotationView.annotation.title)")

        // This line yields a compiler error of: 'MKAnnotation is not convertible to 'CustomMapPinAnnotation'
        var pinannotation : CustomMapPinAnnotation = annotationView.annotation

        println("status was = \(myannotation.status)")
        println("supplierdataindex was = \(myannotation.supplierdataindex)")     
    }
}

回答1:


You will have to typecast it to CustomMapPinAnnotation like this,

func mapView(mapView: MKMapView!, annotationView: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {

    if control == annotationView.rightCalloutAccessoryView {

        if let pinannotation = annotationView.annotation as? CustomMapPinAnnotation{

          println("status was = \(pinannotation.status)")
          println("supplierdataindex was = \(pinannotation.supplierdataindex)")    

       }
    }
}


来源:https://stackoverflow.com/questions/25967783/problems-accessing-custom-mkannotation-data-via-rightcalloutaccessoryview

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!