2011-11-08 104 views
3

我有一些地圖視圖控制器,我有一個自定義註釋。iPhone地圖視圖自定義註釋

自定義註釋代碼:

#import <Foundation/Foundation.h> 
#import <MapKit/MKAnnotation.h> 

@interface DisplayMapAnnotation : NSObject <MKAnnotation> { 

    CLLocationCoordinate2D coordinate; 
    NSString *title; 
    NSString *subtitle; 
    NSString *detailID; 

} 

@property (nonatomic, assign) CLLocationCoordinate2D coordinate; 
@property (nonatomic, retain) NSString *title; 
@property (nonatomic, retain) NSString *subtitle; 
@property (nonatomic, retain) NSString *detailID; 

@end 

#import "DisplayMapAnnotation.h" 

@implementation DisplayMapAnnotation 
@synthesize coordinate, title, subtitle, detailID; 

- (NSString *) title 
{ 

    return title; 

} 

- (NSString *) subtitle 
{ 

    return subtitle; 

} 

- (NSString *) detailID 
{ 

    return detailID; 

} 

- (id)initWithCoordinate:(CLLocationCoordinate2D) c 
{ 

    coordinate=c; 
    return self; 

} 

- (void) dealloc 
{ 

    [title release]; 
    [subtitle release]; 
    [detailID release]; 

    [super dealloc]; 

} 

一個這裏是我創建的註釋我的地圖視圖代碼: 在viewDidLoad方法我做這樣的事情:

for (FeedItems *aItem in geoDataList) { 

      [mapView setZoomEnabled:YES]; 
      [mapView setScrollEnabled:YES]; 
      MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } }; 
      region.center.latitude = [aItem.geoLat doubleValue]; 
      region.center.longitude = [aItem.geoLng doubleValue]; 
      region.span.longitudeDelta = kLatitudeDelta; 
      region.span.latitudeDelta = kLongitudeDelta; 
      [mapView setRegion:region animated:YES]; 



      ann = [[DisplayMapAnnotation alloc] init]; 
      ann.title = aItem.job; 
      ann.subtitle = aItem.jobCompany; 
      ann.detailID = aItem.jobID; 
      ann.coordinate = region.center; 
      [mapView addAnnotation:ann]; 

      [aItem release]; 
     } 

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation 
{ 

    MKPinAnnotationView *pinView = nil; 

    if (annotation != self.mapView.userLocation) { 

     static NSString *defaultPinID = @"com.invasivecode.pin"; 

     pinView = (MKPinAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID]; 

     if (pinView == nil) 
      pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease]; 

     pinView.pinColor = MKPinAnnotationColorRed; 
     pinView.canShowCallout = YES; 
     pinView.animatesDrop = YES; 

     if (mapIdentifier == 0) { 

      UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
      //NSInteger annotationValue = [annView indexOfObject:annotation]; 
      //rightButton.tag = annotationValue; 
      [rightButton addTarget:self action:@selector(detailButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; 
      pinView.rightCalloutAccessoryView = rightButton; 

     } 

    } else { 

     [self.mapView.userLocation setTitle:@"your location"]; 

    } 

    return pinView; 

} 

這裏是我的問題(這種方法):

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control 
{ 

    NSLog(@"Detail id: %@", view.annotation.detailID); 

} 

當我將日誌的詳細id Xco請說:「在'id'類型的對象上找不到'屬性'detailID'」有什麼問題?

+0

@Alexander Farber感謝您爲提高問題質量所做的努力。但請注意,在這裏,許多人都認爲去除稱呼或「感謝」是次要的改變。嘗試進行編輯_substantial_。 –

回答

3

它不知道的註釋是你的自定義註解:

NSLog(@"Detail id: %@", view.annotation.detailID); 

你可以施放像這樣沉默警告

NSLog(@"Detail id: %@", [(DisplayMapAnnotation*)view.annotation detailID]); 

後在檢查,這是一個DisplayMapAnnotation *

if ([view.annotation isKindOfClass:[DisplayMapAnnotation class]]) 
+0

我可以有一些不同的問題嗎?有時我會有少量的註釋。這是行之有效的,但有時我有很多註釋,然後我收到內存警告,我的應用程序崩潰。有一些解決方案如何解決它?坦克...... – mysho

+0

您應該提出一個新問題,並提供有關注釋數量,如何實施dealloc方法以及在獲取mem警告時執行的操作的詳細信息。 – jbat100

相關問題