0

私はMKMapViewアプリにを実装しており、-mapView: viewForAnnotation:動的に配置されたピンとコールアウトをマップ上に表示するためのメソッドを実装しました。私が興味を持っているマップ上の注釈は、Annotationを実装するという別のクラスで定義されていますMKAnnotation。メソッドでは、-mapView: viewForAnnotation:メソッドのannotation変数を介して供給される変数の値を取得する必要があります。これは、のインスタンスになりますAnnotation

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
if ([annotation isKindOfClass:[MKUserLocation class]]) {
    return nil;
}

if ([annotation isKindOfClass:[Annotation class]]) {
    static NSString *identifier = @"RemoteLocation";
    MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
    if (annotationView == nil) {
        annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
    } else {
        annotationView.annotation = annotation;
    }
    annotationView.enabled = YES;
    //HERE, I NEED TO GET THE VALUE OF annotation's VARIABLE IN ORDER TO ASSIGN THE ANNOTATIONVIEW IMAGE
    //annotationView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:annotation.senderPhotoURL]]];
    return annotationView;
    }
return nil;
}

annotation問題は、のインスタンスであるから変数の値を取得しようとすると、Annotation変数にアクセスできないことです。annotationのインスタンスであるプログラムに通知しAnnotationて、変数を読み取れるようにするには、何をする必要がありますか?

4

1 に答える 1

2

これが下にあることを証明しましたannotationが、Annotation次のように、アノテーションの実際のインスタンスにキャストする必要があります。

Annotation* yourAnnotation = (Annotation *)annotation;

その後、yourAnnotation期待どおりに使用できます。

于 2013-01-02T08:37:06.967 に答える