The problem is in how the NSString
properties in the MapItem
class are declared (not that one of the titles is an array as suggested in my comments):
@property (nonatomic, assign) NSString *itemtitle;
@property (nonatomic, assign) NSString *icon;
@property (nonatomic, assign) NSString *itemsubtitle;
By declaring the properties as assign
, the values are not retained by the annotation object and the memory later (when the map view tries to access it) is pointing to something else (in your case it just happened to be something that looked like it was an array).
Change the declarations to this (change assign
to copy
):
@property (nonatomic, copy) NSString *itemtitle;
@property (nonatomic, copy) NSString *icon;
@property (nonatomic, copy) NSString *itemsubtitle;
A separate, unrelated issue you will encounter later is with annotation view re-use:
Right now, in viewForAnnotation
, the image
of the view is only being set when a view is created (when annotationView == nil
) for some annotation "x".
However, if a view is re-used for another annotation "y", the image
will still be set to the one when the view was originally created for annotation "x".
To fix this potential problem, move the image
assignment to after the if-else block:
//Don't assign the image here
//(because it's specific to the current annotation)
//annotationView.image = [UIImage imageNamed:((MapItem *)annotation).icon];
} else {
annotationView.annotation = annotation;
}
//Assign the image here...
//(because it's specific to the current annotation)
annotationView.image = [UIImage imageNamed:((MapItem *)annotation).icon];
return annotationView;