我正在尝试执行通过 MKAnnotation 传递自定义属性(placeId)的简单功能。我已经使用名为“MapViewAnnotation”的自定义类设置了所有内容。
当用户激活 CalloutAccessoryControlTapped 时,我想简单地将 MapViewController 中的附加值传递给 DetailViewController。我可以让标题/副标题工作,但我需要修改我的代码以允许自定义变量。
我已经尝试了一段时间,但无法使其正常工作。任何帮助都会很棒!谢谢!
MapViewAnnotation.h
@interface MapViewAnnotation : NSObject <MKAnnotation> {
NSString *title;
CLLocationCoordinate2D coordinate;
}
@property (nonatomic, copy) NSString *title;
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *subtitle;
- (id)initWithTitle:(NSString *)ttl andCoordinate:(CLLocationCoordinate2D)c2d;
@end
MapViewAnnotation.m
@implementation MapViewAnnotation
@synthesize title, coordinate, subtitle;
- (id)initWithTitle:(NSString *)ttl andCoordinate:(CLLocationCoordinate2D)c2d {
title = ttl;
coordinate = c2d;
subtitle = @"Test Subtitle";
return self;
}
@end
MapViewController.m 中的注释创建- 在这里您可以看到我正在使用字幕传递 placeId(底线)
location.latitude = [dictionary[@"placeLatitude"] doubleValue];
location.longitude = [dictionary[@"placeLongitude"] doubleValue];
newAnnotation = [[MapViewAnnotation alloc] initWithTitle:dictionary[@"placeName"]
andCoordinate:location];
newAnnotation.subtitle = dictionary[@"placeId"];
MapViewController.m 中的 CalloutAccessoryControlTapped - 在这里您可以看到我将 placeId 保存到 NSUserDefaults
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view
calloutAccessoryControlTapped:(UIControl *)control
{
NSString *passedId = view.annotation.subtitle;
[[NSUserDefaults standardUserDefaults]
setObject:passedId forKey:@"passedId"];
[[NSUserDefaults standardUserDefaults] synchronize];
}