0

我有一个 mapView,用户可以在其中按下图钉。一次可能有多个引脚,每个注释视图都有一个标注,在按下时将新视图推送到堆栈。我想要做的是将注释视图的标题传递给第二个视图中的标签。

这是我放下别针的代码:

-(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];
                       address = [NSString stringWithFormat:@"%@ %@, %@ %@", [place subThoroughfare], [place thoroughfare], [place locality], [place administrativeArea]];

                       if (UIGestureRecognizerStateBegan == [recognizer state]) {
                           addressPin = [[MapPoint alloc]initWithCoordinate:touchMapCoordinate
                                                                        title:address];
                           [worldView addAnnotation:addressPin];
                       }
                   }
               }];
}

这是我调用第二个视图的代码:

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

2 回答 2

1

您可以覆盖 MKAnnotation(例如 MyLocation)并在 MyLocation.h 文件中声明

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

@interface MyLocation : NSObject <MKAnnotation> {
    NSNumber *identyfier;
    NSString *_name;
    NSString *_address;
    CLLocationCoordinate2D _coordinate;
}

@property (copy) NSString *name;
@property (copy) NSString *address;
@property (copy) NSNumber *identyfier;
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;

- (id)initWithName:(NSString*)name address:(NSString*)address 
    coordinate:(CLLocationCoordinate2D)coordinate identyfier:(NSNumber *) identyfier;

@结尾

在 MyLocation.m 文件中:

#import "MyLocation.h"

@implementation MyLocation

@synthesize name = _name;
@synthesize address = _address;
@synthesize coordinate = _coordinate;
@synthesize identyfier = _identyfier;

- (id)initWithName:(NSString*)name address:(NSString*)address 
    coordinate:(CLLocationCoordinate2D)coordinate identyfier:(NSNumber *)identyfier {
    if ((self = [super init])) {
        _name = [name copy];
        _address = [address copy];
        _coordinate = coordinate;
        _identyfier = identyfier;
    }
    return self;
}

在您的地图视图中,当您声明注释时,请使用以下方法:

MyLocation *pin = [[MyLocation alloc] initWithName:place.name address:place.address coordinate:coordinate2D identyfier:some_id];

因此,例如在您的地图委托中:

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {

您可以使用:

((MyLocation *)annotation).identyfier

检查选定的注释(当然你可以在 MyLocation 类中使用不同的变量)

于 2012-10-24T22:32:41.610 回答
0

通常,当我需要传递变量时,我只需创建一个全局变量,然后两个视图都可以读取该变量。

extern (NSString *)some_variable_name,将其放入 .h 文件和全局放入的 .m 文件中 (NSString *)some_variable_name ;

所有视图都可以阅读

或者变量前面的 + 号,然后可以被包含该视图的所有视图读取(在 .m 文件的顶部设置全局,在 .h 文件中通过 comman

something = [someview that_variable] ;

于 2012-10-24T22:24:21.340 回答