4

我在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;
}

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

提前致谢。

4

1 回答 1

7

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

出现以下两个问题之一:

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

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

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

    self.mapView.delegate = self;
    

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

    在此处输入图像描述

    如果是这种情况,如果您NSLogviewForAnnotation. delegate如果没有正确设置,您可能根本看不到它被调用。

于 2013-05-26T17:00:51.580 回答