iPhone Dev = maps and deselecting annotations
I am successfully drawing annotations on a map using an array of annotations. I can even click on the annotation and change it’s colour or image. My problem arises when the use selects the second annotation and I want to dynamically change the colour or image of the first one back to a non-selected colour/image. I can get the array of all the annotations and work through the array but once I try to set the colour or i开发者_如何学运维mage ot the array I get a similar error.
for (MKAnnotationView *ann in map.selectedAnnotations){
if ([ann isMemberOfClass:[Place class]]) {
place = (Place *)ann;
if (currentPlaceID != place.placeID) {
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'
Please advise as I have been going around in circles on this one for 2 days now!!!!
Any ideas on how best to do this?
thanks in advance
Do you have a property on the class Place called image?
Something like... @property (nonatomic, retain) UIImage* image;
and is it properly synthesized? @synthesize image;
?
The error is pretty straight forward, some object is receiving a message that it doesn't respond to, namely 'setImage' which is invoked by the .image.
Here is your code:
1. for (MKAnnotationView *ann in map.selectedAnnotations) {
2. if ([ann isMemberOfClass:[Place class]]) {
3. place = (Place *)ann;
4. if (currentPlaceID != place.placeID) {
5. UIImage *i = [UIImage imageNamed:@"pin.png"];
6. ann.image = i;
7. }
8. }
9. }
What I can see:
- ann is an MKAnnotationView (from map.selectedAnnotations)
- you are typecasting your annotation to a place on line 3 (is this right? Does Place subclass MKAnnotationView?)
- you are properly setting the image to the annotation
What this means:
- If Place is indeed a subclass of MKAnnotationView, you hid the setImage (somehow) method
- If Place is NOT a subclass of MKAnnotationView, you've added an invalid annotation to the annotations (sure) that you're trying to treat as an annotation.
I finally figured out how to do this. As usual it's not that hard once you know how. Just thought I would pass this on.
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
NSLog(@"here I am in set selected");
if (YES == selected)
{
NSLog(@"I am selected");
}
else
{
self.backgroundColor = [UIColor clearColor];
NSLog(@"not selected");
}
}
精彩评论