简体   繁体   中英

MKAnnotation: Reading a tag for MKAnnotation

When I add an annotation, I add a ".tag" to it. However I can never read the tag in the following method.

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

    MKAnnotationView *annView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"MyPin"];
    annView.canShowCallout = YES;

    NSLog(@"annView.tag = %d", annView.tag);

    return annView;
}

The NSLog is always 0. Can anyone shed some light on this?

It is worth mentioning that if I NSLog annotation, I do see the tag.

2010-09-10 10:21:27.612 [1328:207] annotation = <BarPin: 0xdbfa5b0; frame = (0 0; 0 0); tag = 99; layer = <CALayer: 0xdbfa680>>

Likely you are setting the tag property on the annotation and trying to read it back on the annotation view.

Also, it looks like your code snippet is leaking annView ; you may want to autorelease that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM