0

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!

Community
  • 1
  • 1

1 Answers1

0

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;
     }
}
Paulw11
  • 108,386
  • 14
  • 159
  • 186
  • I think you mean "vc.time = myPin.time" in the last line of your code. The program crashes and I get "Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MKPinAnnotationView time]: unrecognized selector sent to instance 0x15d25420'" – Michael Chen Aug 18 '14 at 21:08
  • Thanks. Fixed that. Did you change the sender to view.annotation ? – Paulw11 Aug 18 '14 at 21:12