我有一个应用程序(xcode 4.5),该应用程序从闪烁中下载图像。我有一个MapView,可为每张照片的位置丢弃PIN。单击销钉显示出注释,该注释显示了照片的名称和图像的缩略图。我将应用程序通用开始,并且iPad版本按预期工作。但是,当我尝试调整iPhone版本的代码(使用与iPad相同的类)时,PIN不会出现在iPhone的地图视图中。我无法弄清楚我想念的东西,希望有人能提供帮助。这是我所做的。
1)我在iPhone故事板上创建了一个新的视图控制器,并将其连接到了用于iPad的地图视图控制器类。我添加了一个MapView插座并相应地调整了代码。2)在TableView Controller(MAPView Controller的序列)中,我为SEGUE方法添加了一个准备,该方法应该在Map View Controller中使用注释信息更新地图视图。
这是代码:
// 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。谁能告诉我我可能错过了什么?谢谢。
在这里:
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];
}
}
在iPad版本中,您是
- 删除现有注释然后
- 添加新注释
在iPhone版本中,您是
- 添加新注释然后
- 在下一行中删除相同的注释(现有注释)。