Chandler De Angelis
Chandler De Angelis

Reputation: 2776

How to access and pass annotation properties to another view controller

So in my app, I have a mapView that drops pins when the screen is pressed. Once the annotations are dropped, they are placed into an array. There will be multiple pins on the map at one time, and each pin has a identifier property that is an NSNumber. I have another view controller that is pushed onto the stack when the callout button on the annotationView is pressed, and this view has a button that I want to delete the pin from the mapView when pressed. My problem is, I do not know how to pass the identifier of the pin to the second view controller. Here is some code.

This is the method where I drop the pin:

-(void)press:(UILongPressGestureRecognizer *)recognizer
{
  CGPoint touchPoint = [recognizer locationInView:_worldView];
  CLLocationCoordinate2D touchMapCoordinate = [_worldView convertPoint:touchPoint toCoordinateFromView:_worldView];

geocoder = [[CLGeocoder alloc]init];
CLLocation *location = [[CLLocation alloc]initWithCoordinate:touchMapCoordinate
                                                    altitude:CLLocationDistanceMax
                                          horizontalAccuracy:kCLLocationAccuracyBest
                                            verticalAccuracy:kCLLocationAccuracyBest
                                                   timestamp:[NSDate date]];
[geocoder reverseGeocodeLocation:location
               completionHandler:^(NSArray *placemarks, NSError *error) {
                   NSLog(@"reverseGeocoder:completionHandler: called");
                   if (error) {
                       NSLog(@"Geocoder failed with error: %@", error);
                   } else {
                       CLPlacemark *place = [placemarks objectAtIndex:0];
                       geocodedAddress = [NSString stringWithFormat:@"%@ %@, %@ %@", [place subThoroughfare], [place thoroughfare], [place locality], [place administrativeArea]];
                       if (UIGestureRecognizerStateBegan == [recognizer state]) {
                           value = [number intValue];
                           number = [NSNumber numberWithInt:value + 1];
                           addressPin = [[MapPoint alloc]initWithAddress:geocodedAddress coordinate:touchMapCoordinate
                                                                   title:geocodedAddress identifier:number];
                           NSLog(@"The identifier is %@", number);
                           [_annotationArray addObject:addressPin];
                           [_worldView addAnnotation:addressPin];
                           NSLog(@"The number of pins in the annotation array is: %u",_annotationArray.count);
                       }
                   }
               }];
}

This is where I create the second view controller:

-(void)mapView:(MKMapView *)mapView
annotationView:(MKAnnotationView *)view
calloutAccessoryControlTapped:(UIControl *)control
{
  PinViewController *pinViewController = [[PinViewController alloc]init];
  [[self navigationController]pushViewController:pinViewController animated:YES];

  pinViewController.label.text = view.annotation.title;
}

Here is my MKAnnotation class:

#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>

@interface MapPoint : NSObject <MKAnnotation>
{
NSString *_address;
CLLocationCoordinate2D _coordinate;
NSNumber *_identifier;
}

- (id)initWithAddress:(NSString*)address
    coordinate:(CLLocationCoordinate2D)coordinate
         title:(NSString *)t
    identifier:(NSNumber *)ident;


//This is a required property from MKAnnotation
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;

//This is an optional property from MKAnnotataion
@property (nonatomic, copy) NSString *title;
@property (nonatomic, readonly, copy) NSString *subtitle;
@property (nonatomic) BOOL animatesDrop;
@property (nonatomic) BOOL canShowCallout;

@property (copy) NSString *address;
@property (copy, nonatomic) NSNumber *identifier;

@end

#import "MapPoint.h"

@implementation MapPoint

@synthesize title, subtitle, animatesDrop, canShowCallout;
@synthesize address = _address, coordinate = _coordinate, identifier = _identifier;

-(id)initWithAddress:(NSString *)address
   coordinate:(CLLocationCoordinate2D)coordinate
        title:(NSString *)t
   identifier:(NSNumber *)ident
{
self = [super init];

if (self) {
    _address = [address copy];
    _coordinate = coordinate;
    _identifier = ident;

    [self setTitle:t];

    NSDate *theDate = [NSDate date];

    subtitle = [NSDateFormatter localizedStringFromDate:theDate
                                              dateStyle:NSDateFormatterMediumStyle
                                              timeStyle:NSDateFormatterMediumStyle];
    }
return self;
}


@end

Upvotes: 0

Views: 1636

Answers (1)

user427969
user427969

Reputation: 3896

Try following:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
    MyAnnotation *myAnnotation = view.annotation;
}

Just add it as a property in your SecondViewController:

-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
  PinViewController *pinViewController = [[PinViewController alloc]init];
  [[self navigationController]pushViewController:pinViewController animated:YES];

  pinViewController.label.text = view.annotation.title;
  pinViewController.annotation_id = view.annotation.some_id; 
}

Add segue from your first ViewController Try to second ViewController and do check your class like following:

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

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if([segue.identifier isEqualToString: @"segue_name"]) {
        SecondViewController *vc = segue.destinationViewController;
        vc.variable_name = view.annotation.title;
       MyAnnotation vc.annotation_id*myAnnotation = view.annotation.some_id;
    }annotation;
}

Upvotes: 1

Related Questions