2012-12-20 118 views
3

我有一個從Flicker下載圖像的應用程序(Xcode 4.5)。我有一個mapview,它爲每張照片的位置放置一個pin。點擊該針腳會顯示一個註釋,其中顯示照片的名稱和其圖像的縮略圖。我首先開發了應用程序,並且iPad版本按預期工作。但是,當我嘗試調整iPhone版本的代碼時(使用與iPad相同的類),引腳不會出現在iPhone的地圖視圖中。我無法弄清楚我錯過了什麼,我希望有人能夠幫忙。這是我做的。麻煩麻煩地圖註解出現在MapView中

1)我在iPhone故事板中創建了一個新的視圖控制器,並將它連接到適用於iPad的地圖視圖控制器類。我添加了一個mapView插座並相應地調整了代碼。 2)在tableView控制器(它繼承了mapView控制器)中,我添加了一個爲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。誰能告訴我我可能錯過了什麼?謝謝。

回答

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版本中,你是
- 增加新的註解則
- 刪除下一行中的相同註釋(現在的註釋)。

+0

啊!我沒有注意到我翻了代碼。非常感謝!! – jac300