【问题标题】:Problems accessing custom MKAnnotation data via rightCalloutAccessoryView通过 rightCalloutAccessoryView 访问自定义 MKAnnotation 数据时出现问题
【发布时间】:2014-09-22 06:22:26
【问题描述】:

当由 rightCalloutAccessoryView 触发时,我正在尝试访问我在自定义注释数据中设置的一些自定义数据。我收到如下所述的编译器错误。这是我的自定义 MKAnnotation,带有几个额外的变量 - 状态和供应商数据索引

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)

就地图和引脚而言,一切都很好,但现在我想访问 myCustomMapPinAnnotation 数组的自定义部分,特别是“状态”和“供应商数据索引” 这将推动决策以获得更详细的视图。我正在使用 calloutAccessoryControlTapped 来捕捉点击。

问题是这个编译器在下面给我一个错误,我尝试设置访问权限,我认为这会让我指向应该已经内置到我认为的注释数据中的自定义数据。

“MKAnnotation”不能转换为“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)")     
    }
}

【问题讨论】:

    标签: swift mapkit


    【解决方案1】:

    您必须像这样将其类型转换为 CustomMapPinAnnotation,

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

    【讨论】:

    • Tks...正是我需要让我克服这个问题。
    猜你喜欢
    • 1970-01-01
    • 2019-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多