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