iOS5 中 MKAnnotation 未选中

发布于 2024-12-11 20:45:54 字数 499 浏览 0 评论 0原文

我的应用程序在地图上放置一个图钉,然后使用动画选择它,以便用户有视觉线索并可以立即阅读标题/副标题。以下代码在 iOS4 和 iOS5 中都适用,但在 iOS5 中,除非我在 selectAnnotation 方法中将动画更改为“否”,否则不会自动选择注释。

有什么想法吗?

MapAnnotations *pushpin = [[MapAnnotations alloc] initWithCoordinate:coordinate];
pushpin.title = [selectedStation valueForKey:@"name"];
pushpin.subtitle = [selectedStation valueForKey:@"address"];
[stationMap addAnnotation:pushpin];
[stationMap selectAnnotation:pushpin animated:YES];

[pushpin release]; pushpin = nil;

My app places a pushpin on the map and then selects its using animation so the user has a visual clue and can immediately read the title/subtitle. The following code works in both iOS4 and iOS5, but in iOS5, the annotation doesn't get selected automatically unless I change the animation to NO in the selectAnnotation method.

Any ideas why?

MapAnnotations *pushpin = [[MapAnnotations alloc] initWithCoordinate:coordinate];
pushpin.title = [selectedStation valueForKey:@"name"];
pushpin.subtitle = [selectedStation valueForKey:@"address"];
[stationMap addAnnotation:pushpin];
[stationMap selectAnnotation:pushpin animated:YES];

[pushpin release]; pushpin = nil;

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

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

发布评论

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

评论(1

虫児飞 2024-12-18 20:45:54

不知道为什么它之前会起作用,但动画可能需要创建并准备好注释视图,这在添加注释后不太可能立即进行。

您可以做的是将选择移动到 didAddAnnotationViews 委托方法,该方法应该适用于所有 iOS 版本:

- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
{
    for (MKAnnotationView *av in views) {
        if ([av.annotation isKindOfClass:[MapAnnotations class]]) {
            MapAnnotations *pushpin = (MapAnnotations *)av.annotation;
            if (_this_pushpin_is_the_one_to_select) {
                [mapView selectAnnotation:av.annotation animated:YES];
                break;  //or return;
            }
        }
    }
}

Not sure why it would work before but the animation probably requires the annotation view to be created and ready which is unlikely immediately after adding the annotation.

What you can do is move the selection to the didAddAnnotationViews delegate method which should work on all iOS versions:

- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
{
    for (MKAnnotationView *av in views) {
        if ([av.annotation isKindOfClass:[MapAnnotations class]]) {
            MapAnnotations *pushpin = (MapAnnotations *)av.annotation;
            if (_this_pushpin_is_the_one_to_select) {
                [mapView selectAnnotation:av.annotation animated:YES];
                break;  //or return;
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文