0

所以我在我的应用程序的地图上有 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's are like UITableView's。那我怎么称呼它。我忘了指派代表吗?

任何帮助,将不胜感激

4

1 回答 1

0

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

于 2012-07-14T19:44:42.693 回答