0

我对 XCode 和 iPhone 开发非常陌生,所以如果这个问题太简单,请多多包涵。但是我有一张地图,并且我已经成功地为我的注释添加了图像(不是图钉)。当用户选择其中一个注释时,我可以更改图像。

我使用以下方法创建了一个继承自 MKAnnotationView 的类:-

- (id)initWithAnnotation:

- (void)setAnnotation:

- (void)drawRect:

我正在使用

- (void)touchesBegan

知道何时选择了注释。在 touchesBegan 中,我正在做:-

UIImage *i = [UIImage imageNamed:@"A.png"];
self.image = i;

更改图像。但我真正难过的是,当用户选择下一个注释时,如何将图像改回原始图像。我努力了:-

NSArray *selectedAnnotations = map.selectedAnnotations;
for(id annotationView in selectedAnnotations) {
[map deselectAnnotation:[annotationView annotation] animated:NO];
}

但它错误

我试过了

for (MKAnnotationView *ann in map.selectedAnnotations){
if ([ann isMemberOfClass:[Place class]])
{
place = (Place *)ann;
NSLog(@"second = %@"@" %f"@" %f", place.title, place.longitude, place.latitude);
if (currentPlaceID == place.placeID) {
//UIImage *i = [UIImage imageNamed:@"A.png"];

//ann.image = i;
}
else {
UIImage *i = [UIImage imageNamed:@"pin.png"];

ann.image = i;
}
}

}

上面的代码可以正常工作,直到我到达 ann.image = i; 然后它会出错。我得到的错误是: -

*** -[Place setImage:]: unrecognized selector sent to instance 0x4514370
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[Place setImage:]: unrecognized selector sent to instance 0x4514370'

是的,我可以看到我的地点对象没有图像,这就是它出错的原因。但是,如果我在我的地点对象上创建一个图像属性 - 那将如何改变我正在尝试做的注释图像。

请告知,因为我已经在这个圈子里转了 2 天了!!!!

在此先感谢谢丽尔

4

1 回答 1

0

谢丽尔,

我不完全遵循你想要做的事情,但这里有一些想法:

这是我要恢复原始图像的方法:

在您的 MKAnnotationView 子类中,添加两个 UIImage 属性,

firstImage 和 secondImage,设置为retain。

初始化注释视图时,设置两个图像。(在将图像分配给注释视图时,还将其保存到新的 firstImage 属性)

然后,你可以说

self.image = firstImage;

或者

self.image = secondImage.

这会将适当的图像交换到位,同时保留其他图像以进行恢复。

你的代码:

NSArray *selectedAnnotations = map.selectedAnnotations; for(id annotationView in selectedAnnotations) { [map    
deselectAnnotation:[annotationView annotation] animated:NO]; }

是不正确的。它向地图请求一组注释,然后将它们视为注释视图。

注释是一个数据模型对象。它包含描述注释的数据。

注解 VIEW 对象是一个临时显示对象,用于在地图上显示当前可见的注解。地图上的每个注释并不总是有注释视图。

于 2009-11-12T20:56:24.997 回答