0

因此,在我的应用程序中,我有一个 mapView,当屏幕被按下时,它会掉落针脚。一旦注释被删除,它们就会被放入一个数组中。地图上一次会有多个图钉,每个图钉都有一个标识符属性,即 NSNumber。当按下 annotationView 上的标注按钮时,我有另一个视图控制器被推送到堆栈上,并且该视图有一个按钮,我想在按下时从 mapView 中删除 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);
                       }
                   }
               }];
}

这是我创建第二个视图控制器的地方:

-(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;
}

这是我的 MKAnnotation 类:

#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
4

1 回答 1

1

尝试以下操作:

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

只需将其作为属性添加到您的 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; 
}

从你的第一个 ViewController 添加 segue 尝试到第二个 ViewController 并检查你的类,如下所示:

- (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;
}
于 2012-12-03T04:50:12.050 回答