0

そのため、私のアプリには、画面が押されたときにピンをドロップするmapViewがあります。注釈が削除されると、それらは配列に配置されます。マップ上には一度に複数のピンがあり、各ピンにはNSNumberである識別子プロパティがあります。私は、annotationViewのcalloutボタンが押されたときにスタックにプッシュされる別のView Controllerを持っています。このビューには、押されたときにmapViewからピンを削除したいボタンがあります。私の問題は、ピンの識別子を2番目のViewControllerに渡す方法がわからないことです。ここにいくつかのコードがあります。

これは私がピンを落とす方法です:

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

ここで、2番目のViewControllerを作成します。

-(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 からセグエを追加する 2 番目の 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 に答える