Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
392 views
in Technique[技术] by (71.8m points)

iphone - How to keep data associated with MKAnnotation from being lost after a callout pops up and user taps disclosure button?

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.

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

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;
}

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];

}

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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];
}


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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...