iOS - 在MKAnnotationView上设置详细信息披露按钮

时间:2012-02-21 04:41:22

标签: ios uibutton mapkit mkannotation mkannotationview

我的MapViewController中有以下方法:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
    MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"MapVC"];
    if (!annotationView) {
        annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"MapVC"];
        annotationView.canShowCallout = YES;
        annotationView.leftCalloutAccessoryView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
        annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        // could put a rightCalloutAccessoryView here
    } else {
        annotationView.annotation = annotation;
        [(UIImageView *)annotationView.leftCalloutAccessoryView setImage:nil];
    }

    return annotationView;
} 

我相信它已正确设置,但是当我的地图正确显示带有标题和副标题的注释时,但它们没有显示详细信息披露按钮,我是否遗漏了某些内容?

另一件事是,调试此方法时从未调用过,但注释视图会显示标题和副标题。

1 个答案:

答案 0 :(得分:8)

很可能未设置地图视图的delegate

如果未设置delegate或未实现viewForAnnotation方法,则地图视图将创建默认注释视图,该视图是一个红色图钉,其标注仅包含标题和副标题(除非标题是空白的,在这种情况下你会获得一个引脚但没有标注)。

将地图视图的delegate插座连接到文件所有者,或者在代码中添加(例如,在添加注释之前在viewDidLoad中):

mapView.delegate = self;

此外,如果您不使用ARC,请将autorelease添加到alloc行,以避免内存泄漏。