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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…