MKAnnotationView标注视图不会出现

时间:2012-07-14 18:59:45

标签: objective-c ios mkmapview mkannotation mkannotationview

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

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

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

相关问题