如何保持与MKAnnotation相关的数据在标注popup并且用户点击泄露button后丢失?

在用户点击引脚,看到标注并点击揭示button(打开详细视图控制器)之后,如何保存与MKAnnotation对象关联的数据? 我想在详细视图控制器中显示与引脚相关的所有数据。

我有一个简单的MKAnnotation类,如下所示:

#import <Foundation/Foundation.h> #import <MapKit/MapKit.h> @interface VoiceMemoryAnnotation : NSObject <MKAnnotation> { NSString * blobkey; } @property (nonatomic, retain) NSString * blobkey; -(id)initWithBlobkey:(NSString *) key andCoordinate:(CLLocationCoordinate2D) c; @end 

我实现了callback“viewForAnnotation”

 - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation { MKPinAnnotationView*singleAnnotationView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:nil]; // PM: this pin will have a callout (ie dont' forget to override title function! Else exception thrown) singleAnnotationView.canShowCallout = YES; // PM: add disclosure button UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; // PM: when user taps disclosure button, bring them to another page with details about the selected voice memory [rightButton addTarget:self action:@selector(showPinDetails:) forControlEvents:UIControlEventTouchUpInside]; singleAnnotationView.rightCalloutAccessoryView = rightButton; return singleAnnotationView; } 

如果我理解正确,当将VoiceMemoryAnnotation添加到地图对象时,会调用上述方法。 当这个viewForAnnotation被调用时,我只是简单地分配一个MKPinAnnotationView对象并返回它。 当用户点击这个重新调整的引脚,他们看到的标注。 只要他们点击披露button,它会调用“showPinDetails”:

 - (void)showPinDetails:(id)sender { detailViewController = [[MemoryDetailViewController alloc]initWithNibName:@"MemoryDetailViewController" bundle:nil]; [self presentModalViewController:detailViewController animated:YES]; } 

问题是“发件人”对象不包含有关select哪个引脚的任何信息。 有什么方法可以将选定的注释传递给showPinDetails方法吗?

showPinDetails:方法中,您可以从地图视图的selectedAnnotations属性中获取当前选定的注释。

该属性是一个NSArray但由于地图视图只允许一次select一个注释,所以您只需使用索引0处的对象。例如:

 - (void)showPinDetails:(id)sender { if (mapView.selectedAnnotations.count == 0) { //no annotation is currently selected return; } id<MKAnnotation> selectedAnn = [mapView.selectedAnnotations objectAtIndex:0]; if ([selectedAnn isKindOfClass[VoiceMemoryAnnotation class]]) { VoiceMemoryAnnotation *vma = (VoiceMemoryAnnotation *)selectedAnn; NSLog(@"selected VMA = %@, blobkey=%@", vma, vma.blobkey); } else { NSLog(@"selected annotation (not a VMA) = %@", selectedAnn); } detailViewController = [[MemoryDetailViewController alloc]initWithNibName:@"MemoryDetailViewController" bundle:nil]; [self presentModalViewController:detailViewController animated:YES]; } 

使用地图视图的calloutAccessoryControlTapped委托方法可以更容易地直接访问选定的注释,而不是使用自定义button操作方法。 在viewForAnnotation ,删除addTarget并只实现委托方法:

 - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control { id<MKAnnotation> selectedAnn = view.annotation; if ([selectedAnn isKindOfClass[VoiceMemoryAnnotation class]]) { VoiceMemoryAnnotation *vma = (VoiceMemoryAnnotation *)selectedAnn; NSLog(@"selected VMA = %@, blobkey=%@", vma, vma.blobkey); } else { NSLog(@"selected annotation (not a VMA) = %@", selectedAnn); } //do something with the selected annotation... }