【问题标题】:MKAnnotationView Callout View doesn't appearMKAnnotationView 标注视图不出现
【发布时间】:2012-07-14 18:59:45
【问题描述】:

所以我的应用程序中的地图上有 MKAnnotationViews,但是,我似乎无法让注释显示标注。这是我尝试过的一些代码:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {

static NSString *identifier = @"Bar";   
if ([annotation isKindOfClass:[BarLocations class]]) {

    MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
    if (annotationView == nil) {
        annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
    } else {
        annotationView.annotation = annotation;
    }

    annotationView.enabled = YES;
    annotationView.canShowCallout = YES;
    annotationView.animatesDrop = YES;

//Doesn't work
    UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    button.frame = CGRectMake(0, 0, 23, 23);
    annotationView.rightCalloutAccessoryView = button;

//Doesn't work
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
    [leftButton setTitle:annotation.title forState:UIControlStateNormal];
    [annotationView setLeftCalloutAccessoryView:leftButton];
    annotationView.canShowCallout = YES;

//Doesn't work

    UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    rightButton.frame = CGRectMake(0, 0, 15, 15);
    [rightButton addTarget:self
                    action:@selector(showDetails:)
          forControlEvents:UIControlEventTouchUpInside];
    annotationView.rightCalloutAccessoryView = rightButton;


return annotationView; 
}

注意 - 我分别尝试了其中的每一个 - 我只是在这篇文章中将它们放在一起以显示我已经尝试过的选项。

我做错了什么?

编辑

我想我发现了问题的很大一部分:永远不会调用以下方法:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation

但是,我认为MkMapView 就像UITableView 一样。那我怎么称呼它。我忘了指派代表吗?

任何帮助将不胜感激

【问题讨论】:

  • 你要返回 annotationView 吗?
  • 是的 - 注释会显示在我的地图上(带有标题和副标题),只是标注没有
  • 是否实际调用了委托方法,设置 rightCalloutAccessoryView 的行是否实际运行(单步调试器中的方法)?
  • 我正在比较我的一个项目中的代码,它们非常相似。一些不同之处: 1. 我们没有在按钮上设置框架。我们不会从 mapkit 视图中出列(不是出于任何特定原因,尽管文档明确推荐这样做),并且我们还实现了 - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped: (UIControl *)控制
  • 查看我的编辑。我想我正处于解决它的风口浪尖

标签: objective-c ios mkmapview mkannotation mkannotationview


【解决方案1】:

所以问题原来是我忘记将mapView's 委托链接到实现它的类。因此,即使方法存在并且正确,也从未调用过委托方法。一旦我连接了代表,它就完美地工作了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-22
    • 2011-12-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多