MapKit - 详细显示图像CalloutAccessoryView - 访问 mapView.selectedAnnotations 不工作

MapKit - Displaying image in detailCalloutAccessoryView - accessing mapView.selectedAnnotations not working

我有一个 MKPointAnnotation 的子class 叫做 CustomPointAnnotation:

class CustomPointAnnotation: MKPointAnnotation { var imageName: String! var theItemObject: PFObject? var theImage: UIImage? }

在处理地图的视图控制器中(是的,我将 class 设置为 MKMapViewDelegate),我 运行 一个名为 getAnnotations() 的函数成功将我的各种注释添加到地图(恰好是从 Parse 检索到的对象)。每个注释对象都有一个与之关联的图像,即上面class中的theImage

我的困惑是 viewForAnnotationdidSelectAnnotationView 之前是如何被调用的。因此,我无法提取存储在 class 中的图像,我想将其用于标注的 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 一点再往下。

好的,我想我明白了:

我删除了 if/else 检查 mapView.selectedAnnotations。

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

    fullImageOfItem = cpa.theImage

    let myCustomImage: UIImageView = UIImageView(image: fullImageOfItem)

    //anView!.leftCalloutAccessoryView = myCustomImage

    anView!.detailCalloutAccessoryView = myCustomImage

    return anView

我只是将我的 fullImageOfitem 变量设置为 cpa.theImage,这是我的自定义注释 class 的一个实例。在代码的第一行,我将 cpa 设置为注解(自动传递到函数中)作为我的 CustomPointAnnotation。而且它似乎有效!