本文介绍了如何删除mapView上的最后一个注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在.xib文件上添加了一个按钮,我想通过该按钮删除最后添加的注释.

I have added a button on my .xib file with which I want to delete the last annotation that has been added.

因此在Touch-Down操作中,我已经实现了这一点:

So on the Touch-Down action I have implemented this:

-(IBAction)DeleteAnnotation:(id)sender {
   [mapview removeAnnotation:[mapview.annotations lastObject]];
}

我什至尝试过这种方式:

and I have even tried it this way:

-(IBAction)DeleteAnnotation:(id)sender {
   [self.mapview removeAnnotation:self.mapview.annotations.lastObject]];
}

其中mapview是我的MKMapView出口.

我两种方式都遇到的问题是,在删除注释之前,必须多次按下此特定按钮.

The problem I encounter with both ways is that I have to press this specific button quite a few times before an annotation is removed.

此外,注释以相当随机的方式将其删除.

Furthermore, the annotations remove themselves in a quite random way.

我在做错什么吗?或者软件和模拟器有关系吗?

Is there something I am doing wrong or is it a software and simulator matter?

推荐答案

不能保证MKMapViewannotations属性以添加注释的顺序返回注释.

The annotations property of MKMapView is not guaranteed to return the annotations in the same order that you added them.

假设annotations数组属性将以添加注释的顺序返回注释,这很可能是您看到奇怪"行为的原因.请查看这些相关的答案以获取更多详细信息:

Making the assumption that the annotations array property will return the annotations in the same order that you added them is most likely the reason for the "strange" behavior you see. Please see these related answers for some more details:

  • MKMapView annotations changing/losing order?
  • How to reorder MKMapView annotations array


要获得所需的行为(我假设只是删除代码中显式添加的最后一个注释"),这里提供了三种可能的方法(可能还有其他方法):


To get the behavior you want (which I assume is simply "remove the last annotation that was added explicitly by my code"), here are three possible approaches (there may be others):

  1. 最简单的方法是将引用保留在对添加的最后一个注释的强大属性中(调用addAnnotation时更新引用).当您要删除最后添加的注释"时,请将保存的引用传递给removeAnnotation.例如:

  1. Simplest approach is to keep a reference in a strong property to the last annotation you add (update the reference when you call addAnnotation). When you want to remove the "last annotation added", pass that saved reference to removeAnnotation. For example:

//in the interface...
@property (nonatomic, strong) id<MKAnnotation> lastAnnotationAdded;

//in the implementation...

//when you add an annotation:
[mapview addAnnotation:someAnnotation];
self.lastAnnotationAdded = someAnnotation;  //save the reference

//when you want to remove the "last annotation added":
if (self.lastAnnotationAdded != nil)
{
    [mapview removeAnnotation:self.lastAnnotationAdded];
    self.lastAnnotationAdded = nil;
}

  • 另一个选择是遍历地图视图的annotations数组并搜索"last"注释(或您感兴趣的任何属性).一旦有了对最后一个"的引用(不一定是数组中的最后一个对象),就可以在其上调用removeAnnotation.这种方法假定注释对象本身具有一些属性,这些属性使您可以将注释标识为最后一个".这并非总是可能的.

  • Another option is to loop through the map view's annotations array and search for the "last" annotation (or whatever attribute you're interested in). Once you have a reference to the "last" one (which may not necessarily be the last object in the array), you can call removeAnnotation on it. This approach assumes you have some property in the annotation objects themselves that let you identify an annotation as the "last" one. This may not always be possible.

    另一个选择是保留您自己的注释数组,并在每次调用addAnnotation时将注释对象添加到该数组.这类似于仅对最后添加的注释"进行单一引用,只是您以可以依赖的顺序跟踪整个列表.要删除最后一个",您将从数组中获取lastObject而不是从地图视图中获取(假设数组保持该顺序).从地图添加/删除注释时,必须确保阵列保持同步.

    Another option is to keep your own array of annotations and add annotation objects to this array whenever you call addAnnotation. This is similar to keeping the single reference to just the "last annotation added" except you keep track of the entire list in an order you can rely on. To remove the "last" one, you would get lastObject from your array instead of the map view's (assuming you keep the array in that order). You have to make sure to keep your array in sync as you add/remove annotations from the map.

    这篇关于如何删除mapView上的最后一个注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

  • 08-04 09:09
    查看更多