MapKit - 在detailCalloutAccessoryView中显示图像 - 访问mapView.selectedAnnotations不起作用

时间:2016-01-14 00:59:01

标签: swift mapkit callout

我有MKPointAnnotation的子类CustomPointAnnotation

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

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

我的困惑是viewForAnnotation之前调用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设置为更远一点class

1 个答案:

答案 0 :(得分: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。它似乎正在发挥作用!