views:

647

answers:

1

Hi

I am very new to XCode and iPhone development so please bear with me if this question is too simple. But I have a map and I have successfully added images (not pins) to it for my annotations. And I can change the image when the user selects one of the annotations.

I created a class that inherits from MKAnnotationView with the following methods:-

- (id)initWithAnnotation:

- (void)setAnnotation:

- (void)drawRect:

and I am using

- (void)touchesBegan

to know when an annotation has been selected. And in touchesBegan I am doing :-

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

to change the image. But what I am really stumped on is how do I change the image back to it's original image when the users selects the next annotation. I have tried:-

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

but it errors

and I tried

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;
}
}

}

the above code works ok until I get to ann.image = i; then it errors. The errors I get are:-

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

Yes I can see that my place object does not have an image so that's why it is going wrong. But if I create an image property on my place object - how will that change the annotations image which what I am trying to do.

Please advise as I have been going around in circles on this one for 2 days now!!!!

Thanks in advance Cheryl

A: 

Cheryl,

I don't completely follow what you're trying to do, but here are some thoughts:

Here's what I would do to restore the original image:

In your subclass of MKAnnotationView, add two UIImage Properties,

firstImage and secondImage, set up to retain.

When you init the annotation view, set up both images. (At the point where you assign the image to your annotation view, also save it to your new firstImage property)

Then, you can say

self.image = firstImage;

or

self.image = secondImage.

That will swap the appropriate image into place, while keeping the other image around to restore.

Your code:

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

is not right. It asks the map for an array of annotations, and then treats them as annotation VIEWs.

An annotation is a data model object. It contains the data describing an annotation.

An annotation VIEW object is a temporary display object used to display an annotation on the map if it is currently visible. There are not always annotation views for every annotation on the map.

Duncan C