rightCalloutAccessoryView未显示在MKPinAnnotationView上

Jas*_*Lee 4 iphone cocoa cocoa-touch mkmapview ios

我在MKMapView上使用MKPinAnnotationView,标题和副标题正常显示,但未rightCalloutAccessoryView显示.

我试了很多谷歌,但还没有显示出来.我的代码如下.

- (void)addAnnotation
{
    CLLocationCoordinate2D theCoordinate = self.location.coordinate;

    MapAnnotation *annotation = [[MapAnnotation alloc] initWithCoordinate:theCoordinate];
    annotation.title = @"Pin";
    annotation.subtitle = @"More information";

    [self.mapView addAnnotation:annotation];
}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    if ([annotation isKindOfClass:[MapAnnotation class]]) {
        static NSString *reuseIdentifier = @"MyMKPinAnnotationView";
        MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:reuseIdentifier];

        if (!annotationView) {
            annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
            annotationView.pinColor = MKPinAnnotationColorRed;
            annotationView.enabled = YES;
            annotationView.canShowCallout = YES;
            annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        } else {
            annotationView.annotation = annotation;
        }
    }

    return nil;
}
Run Code Online (Sandbox Code Playgroud)

请注意,可以显示标题和副标题.

提前致谢.

Rob*_*Rob 7

查看标题和副标题是注释视图的默认行为.如果你想做任何其他的事情(例如rightCalloutAccessorView),你必须确保viewForAnnotation通过设置你的方式来调用delegateMKMapView,并确保你viewForAnnotation正在返回annotationView你正在创建的.

发生了两个问题之一:

  1. 在所有情况下你viewForAnnotation都会回来nil.确保annotationView在创建/修改后返回.

  2. 如果delegate尚未设置地图视图,则viewForAnnotation根本不会调用当前视图.

    确保你已经设置好了delegate.您可以以编程方式执行此操作:

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

    或者您可以在IB中执行此操作(通过选择连接检查器,右侧面板的最右侧选项卡和control-drag从delegate场景的状态栏):

    在此输入图像描述

    如果是这种情况,如果你NSLog在你的中添加一个语句或断点viewForAnnotation.如果delegate没有正确设置,您可能根本看不到它被调用.