我有一个从 Flicker 下载图像的应用程序(Xcode 4.5)。我有一个地图视图,它为每张照片的位置放置一个图钉。单击图钉会显示一个注释,其中显示照片的名称及其图像的缩略图。我一开始就使应用程序通用,iPad 版本按预期工作。但是,当我尝试调整 iPhone 版本的代码时(使用与 iPad 相同的类),引脚不会出现在 iphone 的地图视图中。我无法弄清楚我错过了什么,我希望有人能提供帮助。这就是我所做的。
1) 我在 iPhone 故事板中创建了一个新的视图控制器,并将其连接到适用于 iPad 的地图视图控制器类。我添加了一个 mapView 插座并相应地调整了代码。2)在tableView控制器(与mapView控制器相接)中,我添加了一个prepare for segue方法,该方法应该使用注释信息更新地图视图控制器中的地图视图。
这是代码:
// 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];
}
这段代码中还有 MKMapViewDelegate 协议的实现,这里我没有包括在内,因为我不需要为 iPhone 更改它们。谁能告诉我我可能错过了什么?谢谢。