If, as you say, "this did work in iOS 6", you should consider it quite fortunate that it did (or seemed to) and this approach of setting the annotation's image should not be relied on under any version.
Although @Ar Ma is correct that the annotation view's annotation
property should be set (in case the view is being re-used), that won't solve the main issue.
The annotation view's image
is set based on the value of categoryIdNumber
which seems to be some variable outside the viewForAnnotation
delegate method.
You cannot assume that:
viewForAnnotation
will be called immediately after you calladdAnnotation
. Even in iOS 6 or earlier, this is not guaranteed.viewForAnnotation
will be called only once for each annotation. The delegate method can be called multiple times for the same annotation as the user pans or zooms the map and the annotation comes back onto the screen.viewForAnnotation
will be called in the same order that you add the annotations. This is a result of points 1 and 2.
I assume that just before you call addAnnotation
, the categoryIdNumber
is set correctly and then based on the above incorrect assumptions, viewForAnnotation
uses categoryIdNumber
to set the image.
What is happening is that viewForAnnotation
is being called by the map view sometime after all or some of the addAnnotation
calls are done at which point categoryIdNumber
is probably the value connected with the last annotation added and all the annotations use the image applicable to the last annotation.
To fix this (regardless of the iOS version), you must put the correct categoryIdNumber
value into each annotation object before calling addAnnotation
.
It looks like your annotation class is MyAnnotation
and you already have a catMapId
property in it.
You must set this property in the annotation before calling addAnnotation
-- not inside the viewForAnnotation
method which is too late. (By the way, you are creating a MyAnnotation
object inside the viewForAnnotation
method which is pointless.)
So where you create and add the annotations (not in viewForAnnotation
):
MyAnnotation* myAnn = [[MyAnnotation alloc] init];
myAnn.coordinate = ...
myAnn.title = ...
myAnn.catMapId = categoryIdNumber; // <-- set catMapId BEFORE addAnnotation
[mapView addAnnotation:myAnn];
Then the code in viewForAnnotation
should be like this:
- (MKAnnotationView *) mapView:(MKMapView *)mapingView viewForAnnotation:(id <MKAnnotation>) annotation
{
annView = nil;
if(annotation != mapingView.userLocation)
{
static NSString *defaultPinID = @"MyAnnId";
annView = (MKAnnotationView *)[mapingView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( annView == nil )
{
annView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] ;
annView.canShowCallout = YES;
}
else
{
//view is being re-used, re-set annotation to current...
annView.annotation = annotation;
}
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton setTitle:annotation.title forState:UIControlStateNormal];
annView.rightCalloutAccessoryView = rightButton;
//Make sure we have a MyAnnotation-type annotation
if ([annotation isKindOfClass:[MyAnnotation class]])
{
//Do not CREATE a local MyAnnotation object here.
//Instead, get the catMapId from the annotation object
//that was PASSED INTO the delegate method.
//MyAnnotation* annotation= [MyAnnotation new];
//annotation.catMapId = categoryIdNumber;
MyAnnotation *myAnn = (MyAnnotation *)annotation;
//The value of the external variable categoryIdNumber is irrelevant here.
//NSLog(@"categoryIdNumber %@",categoryIdNumber);
NSLog(@"myAnn.catMapId %@",myAnn.catMapId);
//Put the NSNumber value into an int to simplify the code below.
int myAnnCatMapId = [myAnn.catMapId intValue];
NSString *imageName = nil;
switch (myAnnCatMapId)
{
case 9:
{
imageName = @"PIN_comprare.png";
break;
}
case 10:
{
imageName = @"PIN_mangiare.png";
break;
}
case 11:
{
imageName = @"PIN_mangiare.png";
break;
}
case 12:
{
imageName = @"PIN_vivere.png";
break;
}
default:
{
//set some default image for unknown cat ids...
imageName = @"default.png";
break;
}
}
annView.image = [UIImage imageNamed:imageName];
NSLog(@"annview %d", myAnnCatMapId);
}
}
return annView;
}