iphone mapview为前面带来了第一个和最后一个注释

Mat*_*cer 8 iphone mapping mkmapview mkannotation

我需要一些可以在mk*函数之外使用的代码.我需要运行我的自定义函数,将FIRST和LAST标记放在一个数组中.(所以在我屏幕上的所有其他标记之上).我试过了

[self.view bringSubviewToFront:[[mapView annotations]objectAtIndex: 0]];
Run Code Online (Sandbox Code Playgroud)

我已经[[mapView annotations]objectAtIndex: 0]在我的代码中使用了它,但是当我尝试将它带到前面时它会崩溃.我是否访问了错误的图层?

谢谢你的帮助.

Ita*_*tay 12

你把错误的东西带到了前面.也就是说,annotations数组是符合MKAnnotation协议(类型id<MKAnnotation>)的对象数组,它们不是视图.

相反,您应该获得所需注释的视图,并将它们放在前面:

id<MKAnnotation> annotation = [[mapView annotations] objectAtIndex:0];
MKAnnotationView* annotationView = [mapView viewForAnnotation:annotation];
if (annotationView != nil) {
    [annotationView.superview bringSubviewToFront:annotationView];
}
Run Code Online (Sandbox Code Playgroud)

但是,您应该注意以下几点:

  1. annotationView可能是nil如果注释位于屏幕外的点上,或者注释尚未完成添加到地图中.但是,如果是这样nil,您可能不关心是否在屏幕上的注释是否在前面.
  2. 你需要调用bringSubviewToFrontannotationView的上海华,而不是self.view甚至mapView,因为这些都不是的的上海华annotationView.