通过rightCalloutAccessoryView访问自定义

通过rightCalloutAccessoryView访问自定义

本文介绍了通过rightCalloutAccessoryView访问自定义MKAnnotation数据时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我被rightCalloutAccessoryView触发时,我正在尝试访问一些在自定义注释数据中设置的自定义数据.我收到了如下所述的编译器错误.这是我的自定义MKAnnotation,其中包含几个额外的变量-status& Supplierdataindex

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)

所有内容在地图上都是不错的,而且是固定的,但是现在我想访问该myCustomMapPinAnnotation数组的自定义部分,特别是"status"和"supplierdataindex"这将推动决策以获得更详细的视图.我正在使用calloutAccessoryControlTapped来捕获点击.

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"不能转换为"CustomMapPinAnnotation"

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

推荐答案

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

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

       }
    }
}

这篇关于通过rightCalloutAccessoryView访问自定义MKAnnotation数据时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 06:48