4

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

1 に答える 1

5

カスタム マップ ビュー アノテーションに新しいプロパティを追加してみませんか? MapViewAnnotation.h追加

@property (nonatomic, strong) NSString *passedID;

次に、viewcontroller での注釈の作成で、サブタイトルを設定する代わりにそのプロパティを設定します。

newAnnotation = [[MapViewAnnotation alloc] initWithTitle:dictionary[@"placeName"]
                                               andCoordinate:location];

newAnnotation.passedID = dictionary[@"placeId"];

最後に、CalloutAccessoryControlTappedで、MapViewAnnotation をカスタム クラスにキャストし、subtitle プロパティではなく、passedID プロパティにアクセスします。

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view 
calloutAccessoryControlTapped:(UIControl *)control
{
    NSString *passedId = ((MapViewAnnotation*)view.annotation).passedID;
    [[NSUserDefaults standardUserDefaults]
    setObject:passedId forKey:@"passedId"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}
于 2013-02-13T11:41:25.173 に答える