3

Flicker から画像をダウンロードするアプリケーション (Xcode 4.5) があります。各写真の場所のピンをドロップするマップビューがあります。ピンをクリックすると、写真の名前とその画像のサムネイルを示す注釈が表示されます。最初からアプリケーションをユニバーサルにしましたが、iPad 版は期待どおりに動作します。ただし、iPhone バージョンのコードを調整しようとすると (iPad の場合と同じクラスを使用して)、iPhone のマップ ビューにピンが表示されません。何を見逃したのかわかりません。誰かが助けてくれることを願っています。これが私がしたことです。

1) iPhone ストーリーボードに新しいビュー コントローラーを作成し、iPad で動作するマップ ビュー コントローラー クラスに接続しました。mapView アウトレットを追加し、それに応じてコードを調整しました。2)tableViewコントローラー(mapViewコントローラーにセグエ)で、マップビューコントローラーのマップビューを注釈情報で更新することになっているセグエメソッドの準備を追加しました。

コードは次のとおりです。

// 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 用に変更する必要がなかったため、ここには含めませんでした。私が見逃したかもしれないことを誰か教えてもらえますか?ありがとう。

4

1 に答える 1

0

ここ:

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 バージョンでは、
新しい注釈を
追加してから、次の行で同じ注釈 (現在は既存の注釈) を削除します。

于 2012-12-20T18:08:51.753 に答える