如何在弹出标注和用户点击公开按钮后保持与MKAnnotation相关的数据丢失?

时间:2012-03-21 00:34:58

标签: iphone mapkit mkannotation

在用户点击图钉,查看标注并点击显示详细视图控制器的显示按钮后,如何保持与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

我实现了回调“viewForAnnotation”

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id     <MKAnnotation>)annotation
{
    MKPinAnnotationView*singleAnnotationView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:nil];


// PM: this pin will have a callout (i.e. 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对象并将其返回。当用户点击这个重新调整的引脚时,他们会看到标注。一旦他们点击了公开按钮,它就会调用“showPinDetails”:

- (void)showPinDetails:(id)sender
{


    detailViewController = [[MemoryDetailViewController alloc]initWithNibName:@"MemoryDetailViewController" bundle:nil];
    [self presentModalViewController:detailViewController animated:YES];

}

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

1 个答案:

答案 0 :(得分:8)

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

该属性是NSArray但由于地图视图一次只允许选择一个注释,因此您只需使用索引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委托方法可以更方便地访问所选注释,而不是使用自定义按钮操作方法。在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... 
}