iOS - 在 MKAnnotationView 上设置详细信息披露按钮

发布于 2025-01-07 16:56:32 字数 1016 浏览 4 评论 0原文

我的 MapViewController 中有以下方法:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
    MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"MapVC"];
    if (!annotationView) {
        annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"MapVC"];
        annotationView.canShowCallout = YES;
        annotationView.leftCalloutAccessoryView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
        annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        // could put a rightCalloutAccessoryView here
    } else {
        annotationView.annotation = annotation;
        [(UIImageView *)annotationView.leftCalloutAccessoryView setImage:nil];
    }

    return annotationView;
} 

我相信它已正确设置,但是当我的地图正确显示带有标题和副标题的注释,但它们不显示详细信息披露按钮时,我是否遗漏了某些内容?

另一件事是,在调试时永远不会调用此方法,但注释视图会显示标题和副标题。

I have the following method in my MapViewController:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
    MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"MapVC"];
    if (!annotationView) {
        annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"MapVC"];
        annotationView.canShowCallout = YES;
        annotationView.leftCalloutAccessoryView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
        annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        // could put a rightCalloutAccessoryView here
    } else {
        annotationView.annotation = annotation;
        [(UIImageView *)annotationView.leftCalloutAccessoryView setImage:nil];
    }

    return annotationView;
} 

I believe it's properly set up, but when my map shows my annotations with title and subtitle properly, but they don't show the detail disclosure button, am I missing something?

Another thing is that when debugging this method is never called, yet the annotation view shows up with title and subtitle.

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

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

发布评论

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

评论(1

[旋木] 2025-01-14 16:56:32

很可能未设置地图视图的delegate

如果未设置 delegate 或未实现 viewForAnnotation 方法,地图视图将创建一个默认注释视图,该视图是一个红色图钉,其标注仅包含标题和副标题(除非标题为空,在这种情况下您将得到一个图钉,但没有标注)。

将地图视图的 delegate 出口连接到文件的所有者,或者在代码中添加此内容(例如,在添加注释之前在 viewDidLoad 中):

mapView.delegate = self;

此外,如果您不使用 ARC ,将 autorelease 添加到 alloc 行以避免内存泄漏。

Most likely the map view's delegate is not set.

If the delegate is not set or if you don't implement the viewForAnnotation method, the map view will create a default annotation view which is a red pin with a callout containing only the title and subtitle (unless the title is blank in which case you will get a pin but no callout).

Either connect the map view's delegate outlet to File's Owner or in code add this (eg. in viewDidLoad before the annotation is added):

mapView.delegate = self;

Also, if you're not using ARC, add autorelease to the alloc lines to avoid a memory leak.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文