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

8vi*_*ius 5 uibutton mapkit mkannotation mkannotationview ios

我的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;
} 
Run Code Online (Sandbox Code Playgroud)

我相信它设置正确,但是当我的地图正确地显示我的带有标题和副标题的注释时,但它们没有显示详细信息披露按钮,我错过了什么吗?

另一件事是,在调试时,从不调用此方法,但注释视图显示标题和副标题.

小智 8

很可能delegate没有设置地图视图.

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

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

mapView.delegate = self;
Run Code Online (Sandbox Code Playgroud)

此外,如果您不使用ARC,请添加autoreleasealloc行以避免内存泄漏.