Right Callout Accessory方法及实现

发布于 2024-12-12 19:16:36 字数 536 浏览 4 评论 0原文

我想知道是否有人可以告诉我如何向地图注释添加正确的标注附件。我尝试的一切似乎都没有取得任何成果,因此任何帮助将不胜感激。

编辑

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

锦欢 2024-12-19 19:16:36

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

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

Objective-C 区分大小写。

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

另外,请确保在添加注释之前设置注释的 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);
}

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

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

Objective-C is case-sensitive.

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

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

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:

- (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.

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

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view 
        calloutAccessoryControlTapped:(UIControl *)control
{
    NSLog(@"accessory button tapped for annotation %@", view.annotation);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文