简体   繁体   中英

How do I access the current annotation's NSString when in “prepareForSegue”?

Pin .h

#import <Foundation/Foundation.h>
@import MapKit;

@interface Pin : NSObject <MKAnnotation> {

NSString *time;
CLLocationCoordinate2D coordinate;
}

@property (nonatomic, copy)   NSString *time;
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;

@end

Pin.m

#import "Pin.h"

@implementation Pin

@synthesize coordinate;
@synthesize time;
@end

How I set my Pin in the view

Pin *newPin = [[Pin alloc]init];
            newPin.coordinate = userCoordinate;

    NSDate *currentTime = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"MM-DD-yyy hh:mm a"];
    NSString *resultString = [dateFormatter stringFromDate: currentTime];
    newPin.time = resultString;
    [self.mapView addAnnotation:newPin];

What Happens when I press the callout

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    [self performSegueWithIdentifier:@"Details" sender:view];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
      if ([segue.identifier isEqualToString:@"Details"])
     {
        InfoViewController *vc = segue.destinationViewController;
     }
}

Now in the second view controller that is accessed with the identifier "Details", I have a NSString that will hold the time of the specific pin. How can I pass the current pin's time NSString to the next view's NSString?

Note: There will but multiple pins set at different times on the map and I have tried Passing data from annotations to detail view iOS using storyboard and tried to use

Pin *an = (Pin *)mapView.annotation;

but it only lets me used an array for the mapView annotation

Pin *an = (Pin *)mapView.annotations;

Please Help!

You can access your annotation via the annotation property of the annotation view and then pass this as the sender to performSegueWithIdentifier like so

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    [self performSegueWithIdentifier:@"Details" sender:view.annotation];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
      if ([segue.identifier isEqualToString:@"Details"])
     {
        Pin *myPin=(Pin *)sender;
        InfoViewController *vc = segue.destinationViewController;
        vc.time=myPin.time;
     }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM