【问题标题】:rightCalloutAccessoryView not shown on the MKPinAnnotationViewMKPinAnnotationView 上未显示 rightCalloutAccessoryView
【发布时间】:2013-05-26 16:56:45
【问题描述】:

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

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

提前致谢。

【问题讨论】:

  • 您的viewForAnnotation 没有返回annotationView。所以请确保返回它(如果它返回nil,无论您的所有工作如何,都会发生默认行为)。请参阅我修改后的答案。

标签: iphone ios cocoa-touch cocoa mkmapview


【解决方案1】:

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

出现以下两个问题之一:

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

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

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

    self.mapView.delegate = self;
    

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

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

【讨论】:

  • 谢谢!天哪……我昨晚很困。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-25
  • 2014-10-18
  • 2016-08-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多