xcode Right Callout 附件方法及实现

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7923961/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:15:35  来源:igfitidea点击:

Right Callout Accessory method and implementation

iosxcodemapannotationsmapkit

提问by James Kerstan

I was wondering if anyone could tell me how to add a right callout accessory to a map annotation. everything i try doesn't seem to be getting anywhere, so any help would be appreciated.

我想知道是否有人可以告诉我如何将正确的标注附件添加到地图注释中。我尝试的一切似乎都无济于事,因此将不胜感激。

EDIT

编辑

I have tried this line of code but nothing different happens to the annotation.

我已经尝试过这行代码,但注释没有任何不同。

- (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;
}

回答by

The method name is wrong. It should be mapViewwith a capital V:

方法名称错误。它应该是mapView大写的V

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

Objective-C is case-sensitive.

Objective-C 区分大小写。

If the method still doesn't get called, then the other problem is that the map view's delegateis not set. In code, set it to selfor in Interface Builder attach the delegate to File's Owner.

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

Also make sure you set the titleof the annotation before adding it otherwise the callout still won't show.

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

The above changes should fix the accessory button not appearing.

上述更改应修复不出现的附件按钮。


Some other unrelated suggestions...


其他一些不相关的建议...

In viewForAnnotation, you should support annotation view re-use by calling dequeueReusableAnnotationViewWithIdentifier:

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

If your project uses ARC, remove the autorelease.

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


By the way, to respond to the accessory button press, implement the calloutAccessoryControlTappeddelegate method:


顺便说一下,要响应附件按钮的按下,请实现calloutAccessoryControlTapped委托方法:

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