我希望能够使用MKPointAnnotation
为每个丢弃的引脚存储自定义值。具体来说,我想在每个引脚上存储一些id并在calloutAccesoryControlTapped
检索。
答案 0 :(得分:2)
您必须使用属性子类化MKPointAnnotation来存储此自定义值(我将其命名为标记)
import UIKit
import MapKit
class CustomPointAnnotation: MKPointAnnotation {
var tag: Int!
}
创建别针:
let annotation = CustomPointAnnotation()
annotation.coordinate = CLLocationCoordinate2D(latitude: CLLocationDegrees(latitude), longitude: CLLocationDegrees(longitude))
annotation.title = [insert name]
annotation.tag = [insert tag]
self.mapView.addAnnotation(annotation)
在mapView委托的viewForAnnotation中,检查了dequeableAnnotation后:
if (annotation is CustomPointAnnotation) {
pinView?.tag = (annotation as! CustomPointAnnotation).tag
}
答案 1 :(得分:1)
对Marcos Griselli answer进行小修改。需要强制转换pinView才能访问自定义标记。
if (annotation is CustomPointAnnotation) {
(pinView?.annotation as! CustomPointAnnotation).tag = (annotation as! CustomPointAnnotation).tag
}