0

我有一个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更远一点。

4

1 回答 1

0

好的,所以我想我想通了:

我删除了 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为我的自定义注释类的一个实例。在代码的第一行中,我将cpa注解(自动传递给函数)设置为我的CustomPointAnnotation. 它似乎正在工作!

于 2016-01-14T16:28:46.953 回答