2011-11-30 84 views
2

我似乎無法將披露按鈕添加到我的地圖註釋中。添加披露指標來映射引腳iOS 5

我實現了MKMapViewDelegate到我的視圖控制器。我錯過了什麼?

- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation 
{ 
    MKPinAnnotationView *mapPin = nil; 
    if(annotation != map.userLocation) 
    { 
     static NSString *defaultPinID = @"defaultPin"; 
     mapPin = (MKPinAnnotationView *)[map dequeueReusableAnnotationViewWithIdentifier:defaultPinID]; 
     if (mapPin == nil) 
      mapPin = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID]; 

     mapPin.canShowCallout = YES; 
     UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
     mapPin.rightCalloutAccessoryView = infoButton; 

    } 
    return mapPin; 
} 

回答

3

該代碼應該工作,但檢查以下內容:

  • 是地圖視圖的delegate屬性集?聲明視圖控制器實現MKMapViewDelegate協議實際上並不告訴地圖視圖哪個對象正在實現委託方法。在代碼中,執行map.delegate = self;或IB將delegate插座連接到文件所有者。
  • 註釋的title屬性是否設置爲非空白?如果title爲空,則不會顯示標註。

另外,無關但是,當離隊返回註釋視圖,您應該更新其annotation屬性設置爲當前註釋(它可能已被用於另一個註解之前)。另外,除非你使用ARC,否則你也應該看到autorelease

- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation 
{ 
    MKPinAnnotationView *mapPin = nil; 
    if(annotation != map.userLocation) 
    { 
     static NSString *defaultPinID = @"defaultPin"; 
     mapPin = (MKPinAnnotationView *)[map dequeueReusableAnnotationViewWithIdentifier:defaultPinID]; 
     if (mapPin == nil) 
     { 
      mapPin = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation 
         reuseIdentifier:defaultPinID] autorelease]; 
      mapPin.canShowCallout = YES; 
      UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
      mapPin.rightCalloutAccessoryView = infoButton; 
     } 
     else 
      mapPin.annotation = annotation; 

    } 
    return mapPin; 
} 
+0

謝謝安娜,問題是代表。我忘了正確更新它。 – MrShoot