右侧附加视图访问方法及实现

12
我想知道如何将右侧的呼叫注释添加到地图标注中。我尝试了很多方法,但好像没有用。非常感谢任何帮助。
编辑:我尝试了这行代码,但是注释没有发生任何变化。
- (MKAnnotationView *)mapview:(MKMapView *)sender viewForAnnotation:(id <MKAnnotation>)annotation 
{
MKAnnotationView *aView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@""];
aView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
aView.canShowCallout = YES;
aView.annotation = annotation;
return aView;
}

你能展示一下你尝试过的内容以及具体出现了什么错误或问题吗? - user467105
1个回答

34
方法名错误。应该将mapViewV大写:
- (MKAnnotationView *)mapView:(MKMapView *)sender 
            viewForAnnotation:(id <MKAnnotation>)annotation 

Objective-C是区分大小写的。

如果方法仍然没有被调用,那么另一个问题就是地图视图的delegate没有设置。在代码中,将其设置为self或在Interface Builder中将委托附加到File's Owner。

还要确保在添加注释之前设置注释的title,否则气泡仍然不会显示。

上述更改应该可以解决附件按钮不出现的问题。


一些其他无关的建议...

viewForAnnotation中,您应该通过调用dequeueReusableAnnotationViewWithIdentifier来支持注释视图的重用:

- (MKAnnotationView *)mapView:(MKMapView *)sender viewForAnnotation:(id < MKAnnotation >)annotation
{
    static NSString *reuseId = @"StandardPin";

    MKPinAnnotationView *aView = (MKPinAnnotationView *)[sender 
                dequeueReusableAnnotationViewWithIdentifier:reuseId];
    if (aView == nil)
    {
        aView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation 
                    reuseIdentifier:reuseId] autorelease];
        aView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        aView.canShowCallout = YES;
    }

    aView.annotation = annotation;

    return aView;   
}

如果您的项目使用ARC,请删除autorelease


顺便提一下,要响应附属按钮的按下操作,请实现calloutAccessoryControlTapped委托方法:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view 
        calloutAccessoryControlTapped:(UIControl *)control
{
    NSLog(@"accessory button tapped for annotation %@", view.annotation);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接