I have an application (Xcode 4.5) which downloads images from Flicker. I have a mapview which drops a pin for the location of each photo. Clicking the pin reveals an annotation which shows the name of the photo and a thumbnail of its image. I made the application universal to begin with, and the iPad version works as expected. However, when I attempted to adjust the code for the iPhone version, (using the same classes as I was for iPad) the pins do not appear in the map view for the iphone. I can’t figure out what I missed and I am hoping someone can help. Here is what I did.
1) I created a new view controller in the iPhone storyboard and hooked it up to my map view controller class that is working for iPad. I added a mapView outlet and adjusted code accordingly.
2) In the tableView controller (which segues to mapView controller) I added a prepare for segue method that is supposed to update the map view in the map view controller with the annotation info.
here is the code:
// in the tableView controller
- (NSArray *)mapAnnotations
{
NSMutableArray *annotations = [NSMutableArray arrayWithCapacity:[self.photos count]];
for (NSDictionary *photo in self.photos) {
[annotations addObject:[FlickrPhotoAnnotation annotationForPhoto:photo]];
}
return annotations;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"iPhoneMap"]) {
id destinationViewController = segue.destinationViewController;
if ([destinationViewController isKindOfClass:[MapViewController class]])
{
MapViewController *mapVCIphone =
(MapViewController *)destinationViewController;
mapVCIphone.delegate = self;
mapVCIphone.annotations = [self mapAnnotations];
}
}
}
//in the mapView controller:
@property (weak, nonatomic) IBOutlet MKMapView *mapViewIphone;
@synthesize mapViewIphone = _mapViewIphone;
- (void)updateMapView
{
//if there are any current annotatons, remove them
//if there exists a new array of annotations, then add those to the map
if (self.splitViewController) {
if (self.mapView.annotations) [self.mapView
removeAnnotations:self.mapView.annotations];
if (self.annotations) [self.mapView addAnnotations:self.annotations];
} else {
if (self.annotations) [self.mapViewIphone addAnnotations:self.annotations];
if (self.mapViewIphone.annotations) [self.mapViewIphone
removeAnnotations:self.mapViewIphone.annotations];
}
}
- (void)setAnnotations:(NSArray *)annotations
{
_annotations = annotations;
[self updateMapView];
}
- (void)setMapView:(MKMapView *)mapView //iPad mapView
{
_mapView = mapView;
[self updateMapView];
}
- (void)setMapViewIphone:(MKMapView *)mapViewIphone
{
_mapViewIphone = mapViewIphone;
[self updateMapView];
}
Also in this code is the implementation of the MKMapViewDelegate protocols, which I did not include here as I did not need to change them for iPhone. Can anyone tell me what I might have missed? Thanks.
Here:
In the iPad version you are
– REMOVING existing annotations then
– ADDING new annotations
In the iPhone version you are
– ADDING new annotations then
– REMOVING those same annotations (now existing annotations) in the next line.