如何根据其属性值加载不同的自定义引脚或标识符? [英] How do I load different custom pins or identifiers based off of their property values?

查看:105
本文介绍了如何根据其属性值加载不同的自定义引脚或标识符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个符合<MKAnnotation>的对象数组. 我使用addAnnotations:将此数组加载到我的注释中.

I have an array of objects that conform to <MKAnnotation>. I load this array into my annotations using addAnnotations:.

在方法中:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:    
(id<MKAnnotation>)annotation

我让每个大头针都使用以下方式加载自定义图片:

I have every pin load a custom image using:

annotationView.image = [UIImage imageNamed:@"purp_pin.png"];

但是,我不希望所有的引脚都加载该图像.我希望它根据符合<MKAnnotation>的对象具有的属性来加载不同的自定义图像/标识符.

However, I don't want all the pins to load with this image. I want it to load a different custom image/identifier depending on the properties the object that conformed to <MKAnnotation> had.

我该怎么做?

推荐答案

如果对象中有一些符合MKAnnotation的自定义属性,则可以在viewForAnnotation中访问它以设置图像的一种方式是这样的:

If you have some custom property in your object that conforms to MKAnnotation, one way you can access it in viewForAnnotation to set the image is like this:

MyAnnotationClass *myAnnot = (MyAnnotationClass *)annotation;

if (myAnnot.someProperty == 42)
    annotationView.image = [UIImage imageNamed:@"purp_pin.png"];
else
    annotationView.image = [UIImage imageNamed:@"default.png"];

请确保设置了image属性,而不管注释视图是否已出队或已创建.

Make sure the image property is set regardless of whether annotation view is being dequeued or created.

这篇关于如何根据其属性值加载不同的自定义引脚或标识符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆