【问题标题】:How to close a callout for MKAnnotation in a MKMapView如何在 MKMapView 中关闭 MKAnnotation 的标注
【发布时间】:2009-07-28 12:59:29
【问题描述】:

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

我试过了

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

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

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

【问题讨论】:

    标签: iphone objective-c


    【解决方案1】:

    selectedAnnotation 中的对象是 MKAnnotation 的实例

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

    【讨论】:

    • 我不确定他们是否在您回答后更改了 SDK,但 selectedAnnotations 确实是注释,而不是 MKAnnotationView 的实例。
    • jowie 是对的(但我不确定 2009 年 af. 回复时的情况如何)。我需要编写这样的代码才能使其工作: for(id annotation in selectedAnnotations) { [_mapView deselectAnnotation:annotation animated:NO]; }
    • 我在 [mapView selectedAnnoations ] 中只有一个注释,但 callOut 仍然可见
    • 我还需要明确关闭标注 UIView,否则它不会消失 popupView.removeFromSuperview()
    【解决方案2】:

    如果您想坚持使用地图套件文档。

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

    【讨论】:

    • 太棒了。我想在从除该注释的详细视图之外的任何其他屏幕返回地图时自动关闭标注,因此我向我的视图控制器添加了一个名为 cerrarCallout 的变量和此方法 - (void) viewWillAppear:(BOOL)animated { if (cerrarCallout ) { for (NSObject *annotation in [self.mapView selectedAnnotations]) { [self.mapView deselectAnnotation:(id )annotation animated:NO]; } } cerarCallout = YES;还在详细视图控件的 prepareForSegue 分支中添加了 viewDidLoad 中的 cerarCallout=YES 和 cerarCallout=NO。
    【解决方案3】:

    斯威夫特

    以编程方式关闭(取消选择)所有注释...

    mapView.selectedAnnotations.forEach({ mapView.deselectAnnotation($0, animated: false) })
    

    【讨论】:

      【解决方案4】:
      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)
      }
      

      在切换到新视图控制器之前取消选择注释。这样你回来时它就消失了。

      【讨论】:

        【解决方案5】:

        在 viewWillAppear:animated 中使用以下 hacky 方法代替了一个很好的解决方案

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

        selectedAnnotations 数组确实有 1 个值,但取消选择该值仍然没有关闭调用?所以我只是遍历所有注释并选择和取消选择。我没有很多注释,所以性能影响可能还不错?

        如果有人有更好的想法,我将不胜感激?

        【讨论】:

          【解决方案6】:

          当您重新单击图钉时,标注应该会消失...

          【讨论】:

            【解决方案7】:
            - (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];
                }
            
            }
            

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2015-02-15
              相关资源
              最近更新 更多