0

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

1 Answer 1

1

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;
}
7
  • I am not familiar with segues. If I implement the segue, the identifier property of the annotation will be passed to the second view controller? I don't see where that happens. Dec 3, 2012 at 22:50
  • Its not passed automatically, you need to save it in a variable in SecondViewController like shown in my answer as annotation_id
    – user427969
    Dec 3, 2012 at 22:51
  • I have tried the first solution, but it is not working. It says that the identifier property is not found on object type MKAnnotation, even though I declared it as a property of my MKAnnotation class. Dec 4, 2012 at 1:07
  • I am getting more errors. Is there something wrong with my identifier declaration in the MapPoint class? Ot should I use a different method to pass the NSNumber? Dec 10, 2012 at 2:49
  • What errors are you getting and where? Please update related code changes in your question.
    – user427969
    Dec 10, 2012 at 21:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.