2009-07-28 52 views
14

我有一个MKMapView,它有许多注释。选择引脚显示标注并按下附件将一个新的视图控制器弹出到堆栈上。但是,当我从新的VC中按回时,标注仍处于打开状态。我如何关闭它?如何在MKMapView中关闭MKAnnotation的标注

我已经试过

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

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

我需要添加一些东西到我的MKAnnotation实现或我的MKPinAnnotationView?

回答

29

在selectedAnnotations的对象是要坚持使用地图套件文档MKAnnotation

NSArray *selectedAnnotations = mapView.selectedAnnotations; 
for(id annotation in selectedAnnotations) { 
    [mapView deselectAnnotation:annotation animated:NO]; 
} 
+6

我不知道他们是否已经改变了SDK,因为你回答了这个问题,但selectedAnnotations确实是注解而不是MKAnnotationView的实例。 – jowie 2011-08-22 09:31:35

+1

jowie是对的(但我不确定2009年情况是什么时候回答)。我需要编写这样的代码才能使其工作:for(id selected Annotation in selectedAnnotations){_mapView deselectAnnotation:annotation animated:NO]; } – 2013-05-25 08:33:56

0

当你reclick引脚标注应该走了......

+3

谢谢,但我需要以编程方式关闭它 – joneswah 2009-08-01 13:19:19

1

代替很好的解决方案如下哈克方法在viewWillAppear中工作的:动画

for(MyMapAnnotation *aMKAnn in [myMapView annotations]) 
    { 
     //dodgy select then deselect each annotation 
     [myMapView selectAnnotation:aMKAnn animated:NO]; 
     [myMapView deselectAnnotation:aMKAnn animated:NO]; 
    } 

的selectedAnnotations阵列拥有1价值,但取消选择该价值仍然没有结束呼吁?所以我只是遍历所有注释并选择和取消选择。我没有很多注释,因此性能可能不会太差。

如果有人有更好的想法,我将不胜感激优雅的解决方案吗?

14

的情况下,以备不时之需。

for (NSObject<MKAnnotation> *annotation in [mapView selectedAnnotations]) { 
    [mapView deselectAnnotation:(id <MKAnnotation>)annotation animated:NO]; 
} 
0
- (void)deselectAllAnnotations 
{ 

    NSArray *selectedAnnotations = [self.mapViewObj.mapView selectedAnnotations]; 
    for (int i = 0; i < [selectedAnnotations count]; i++) { 
     [self.mapViewObj.mapView deselectAnnotation:[selectedAnnotations objectAtIndex:i] animated:NO]; 
    } 

} 

这可能会帮助您解决问题。

2
func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, 
      calloutAccessoryControlTapped control: UIControl) 
{ 
    let pin = view.annotation 
    mapView.deselectAnnotation(pin, animated: false) 
    performSegueWithIdentifier("Next VC Segue", sender: nil) 
} 

取消选择Segue公司新的视图控制器之前注释。这样,当你回来时它会消失。