我有一个MKPointAnnotation
名为的子类CustomPointAnnotation
:
class CustomPointAnnotation: MKPointAnnotation {
var imageName: String!
var theItemObject: PFObject?
var theImage: UIImage?
}
在处理地图的视图控制器中(是的,我将类MKMapViewDelegate
设置getAnnotations()
为 每个注释对象都有一个与之关联的图像,即theImage
上面类中的图像。
我的困惑是如何viewForAnnotation
调用 before didSelectAnnotationView
。因此,我无法提取存储在类中的图像,我想将其用于detailCalloutAccessoryView
标注。
以下是我的viewForAnnotation
功能:
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
if !(annotation is CustomPointAnnotation) {
return nil
}
print("viewForAnnotation was called!")
let reuseId = "test"
var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
if anView == nil {
anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
anView!.canShowCallout = true
}
else {
anView!.annotation = annotation
}
//Set annotation-specific properties **AFTER**
//the view is dequeued or created...
let cpa = annotation as! CustomPointAnnotation
anView!.image = UIImage(named:cpa.imageName)
let rightButton: UIButton = UIButton(type: .DetailDisclosure)
rightButton.setImage(UIImage(named: "arrow-icon.png"), forState: .Normal)
rightButton.tintColor = UIColor.grayColor()
rightButton.addTarget(self, action: Selector("showItemDetail:"), forControlEvents: UIControlEvents.TouchUpInside)
anView!.rightCalloutAccessoryView = rightButton
print("about to print the array of selected annotations")
print(mapView.selectedAnnotations)
if mapView.selectedAnnotations.count > 0 {
print("THIS IS NEVER SATISFIED, I WILL NEVER GET HERE, WHY?!")
//yes, there is an annotation currently selected
if let selectedAnnotation = mapView.selectedAnnotations[0] as? CustomPointAnnotation {
fullImageOfItem = selectedAnnotation.theImage
print("i just set the image")
}
}
else {
print("there are no selected annotations")
}
let myCustomImage: UIImageView = UIImageView(image: fullImageOfItem)
anView!.detailCalloutAccessoryView = myCustomImage
return anView
}
我不明白为什么mapView.selectedAnnotations.count
总是0。因此,我无法设置我的fullImageOfItem
变量,然后将其设置为detailCalloutAccessoryView
更远一点。