如何在MKMapView中关闭MKAnnotation的标注

jon*_*wah 17 iphone objective-c

我有一个MKMapView,它有许多注释.选择引脚会显示标注,按下附件会将新的视图控制器弹出到堆栈中.但是,当我从新的VC按回来时,标注仍然打开.我怎么关闭它?

我试过了

if([[myMapView selectedAnnotations] count] > 0)
{
    //deselect that annotation
    [myMapView deselectAnnotation:[[myMapView selectedAnnotations] objectAtIndex:0] animated:NO];
}
Run Code Online (Sandbox Code Playgroud)

但这不起作用.selectedAnnotations在数组中只有一个条目,因此它会进入此语句,但不会关闭标注.

我是否需要在MKAnnotation实现或MKPinAnnotationView中添加内容?

小智 36

selectedAnnotations中的对象是MKAnnotation的实例

NSArray *selectedAnnotations = mapView.selectedAnnotations;
for(id annotation in selectedAnnotations) {
    [mapView deselectAnnotation:annotation animated:NO];
}
Run Code Online (Sandbox Code Playgroud)

  • 我不确定他们是否已更改SDK,因为您回答了这个问题,但selectedAnnotations确实是注释而不是MKAnnotationView的实例. (6认同)

lev*_*han 13

如果您想坚持使用地图工具包文档.

for (NSObject<MKAnnotation> *annotation in [mapView selectedAnnotations]) {
    [mapView deselectAnnotation:(id <MKAnnotation>)annotation animated:NO];
}
Run Code Online (Sandbox Code Playgroud)