如何在弹出标注和用户点击公开按钮后保持与MKAnnotation关联的数据丢失? [英] How to keep data associated with MKAnnotation from being lost after a callout pops up and user taps disclosure button?

查看:87
本文介绍了如何在弹出标注和用户点击公开按钮后保持与MKAnnotation关联的数据丢失?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在用户点击图钉,查看标注并点击显示详细视图控制器的显示按钮后,如何保持与MKAnnotation对象关联的数据?我想在详细视图控制器中显示与引脚相关的所有数据。

How do I keep data associated with an MKAnnotation object after the user taps the pin, sees a callout, and taps the disclosure button which opens a detailed view controller? I want to display all data associated with the pin in the detail view controller.

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

I have a simple MKAnnotation class that looks like:

#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

I implemented the call back "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:

If I understand correctly, the above method is called when you add a VoiceMemoryAnnotation to a map object. When this viewForAnnotation is called, I simply allocate a MKPinAnnotationView object and return it. When the user taps this retuned pin, they see the callout. As soon as they click the disclosure button it calls "showPinDetails":

- (void)showPinDetails:(id)sender
{


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

}

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

The problem is the "sender" object does not contain any information about which pin was selected. Is there some way I can pass in the selected annotation to the showPinDetails method?

推荐答案

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

In the showPinDetails: method, you can get the currently selected annotation from the map view's selectedAnnotations property.

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

That property is an NSArray but since the map view only allows one annotation to be selected at a time, you would just use the object at index 0. For example:

- (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 并实现委托方法:


Instead of using a custom button action method, it can be easier to use the map view's calloutAccessoryControlTapped delegate method which lets you get access to the selected annotation more directly. In viewForAnnotation, remove the addTarget and just implement the delegate method:

- (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... 
}

这篇关于如何在弹出标注和用户点击公开按钮后保持与MKAnnotation关联的数据丢失?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆