简体   繁体   English

MKAnotnotation保留了旧信息

[英]MKAnnotation is persisting with old information

I'm developing an App where a farmer drops custom annotations ( pins with numbers ) at points of interest in his crop, emails a report with screenshot back to his office, and moves on to his next paddock. 我正在开发一个应用程序,农夫可以在农作物的兴趣点上放下自定义注释(带有数字的大头针),然后将带有屏幕截图的报告通过电子邮件发送回他的办公室,然后转到下一个围场。

引脚区域的部分屏幕截图

Part of the code that sends the email include resetting properties and zeroing arrays and ivars etc. His first field works fine, but the numbering of the POI in all the fields that follow go haywire. 发送电子邮件的部分代码包括重置属性以及归零数组和ivars等。他的第一个字段可以正常工作,但随后的所有字段中的POI编号都很麻烦。 The data represented by the errant pins are correct, just the pins themselves are not( more on that in a moment >>**). 错误的引脚表示的数据是正确的,只是引脚本身不是正确的(稍后更多>> **)。

So I've established that there is nothing wrong with anything leading up to a pin drop and I've cleared my annotations with: 因此,我确定导致引脚掉落的任何东西都没有问题,并且使用以下命令清除了注释:

[ self.mapView removeAnnotations:self.mapView.annotations];

The Apple docs mention that one also needs to implement this method in the reset: 苹果文档提到,还需要在重置中实现此方法:

[ self.mapView dequeueReusableAnnotationViewWithIdentifier:@"myAnnotation"]; 

because even though they've been cleared from the screen they are still present in memory. 因为即使从屏幕上清除了它们,它们仍然存在于内存中。 However that still does not fix my quandry. 但是,这仍然不能解决我的问题。 If I explicitly wipe my annotations out of existence with: 如果我通过以下方式明确删除了我的注释:

self.mapView.annotations = nil;

the problem still remains. 问题仍然存在。 Numbers on the annotation appear random in the 2nd and 3rd .. field. 注释上的数字在第二和第三字段中随机出现。 By logging to an onscreen textView I can see the array holding the correct values of the POI number. 通过登录到屏幕上的textView,我可以看到包含正确POI值的数组。 Something about CLLocation or MKAnnotation is still persisting somewhere. 关于CLLocation或MKAnnotation的某些内容仍然存在。 Nothing is in the Class Reference at Apple about how to reset CLLocationManager, so I presume it is done with a simple Start and Stop. Apple的“类参考”中没有关于如何重置CLLocationManager的任何内容,因此我认为它是通过简单的“开始”和“停止”完成的。 My use of those are correctly balanced, so I'm not running multiple instances. 我对这些元素的使用是正确平衡的,因此我没有运行多个实例。

**>> Here's the snippet that decides what pin to drop, the action happens in the middle where commented ** >>这是决定放置哪个引脚的代码段,操作发生在注释的中间

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation(id<MKAnnotation>)annotation
{
if([annotation isKindOfClass:[MKUserLocation class]])
    return nil;

    static NSString *identifier = @"myAnnotation";
MKAnnotationView * annotationView = (MKAnnotationView*)[self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];

if (!annotationView)
{
    annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];


            // standard stuff up to here
            // findings build up as a score of each test point.

    if (contentScore == 0)
    {
            // a green asterisk marker if no score was achieved

    annotationView.image = [UIImage imageNamed:@"markerZero.png"];    
    } else {

            // the marker number comes from idx in SiteCount Array

    str = [NSString stringWithFormat:@"marker%d.png",siteCount[idx];  
    annotationView.image = [UIImage imageNamed:str];
    }


} else {
  annotationView.annotation = annotation;
}

return annotationView;
}

Currently the workaround is the fiddly job of taking it out of memory on the home screen and relaunching before beginning the next field. 当前,解决方法是一项繁琐的工作,即将它从主屏幕上移出内存,然后在开始下一个领域之前重新启动。 I could go back to using the red and green pins provided by the system but he's been spoiled by having numbers to cross-reference the report. 我可以回到使用系统提供的红色和绿色图钉的位置,但是由于交叉引用该报表,他被宠坏了。

So where should I be looking? 那我应该去哪里找? Who is the culprit? 谁是罪魁祸首? I suspect MKAnnonation but my knowledge has run out 我怀疑是MKAnnonation,但我的知识已经用尽

As @Anna said, you are not completely reinitialising you annotation view if a reusable view is dequeued - 正如@Anna所说,如果可重用视图出队,您将不会完全重新初始化注释视图-

You should have something like 你应该有这样的东西

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation(id<MKAnnotation>)annotation
{
    if([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

    static NSString *identifier = @"myAnnotation";

    MKAnnotationView * annotationView = (MKAnnotationView*)[self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];

    if (!annotationView) {
        annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
    }                       //Note this closing brace!

            // standard stuff up to here
            // findings build up as a score of each test point.

    if (contentScore == 0) {
            // a green asterisk marker if no score was achieved
        annotationView.image = [UIImage imageNamed:@"markerZero.png"];    
    } else {
            // the marker number comes from idx in SiteCount Array
        str = [NSString stringWithFormat:@"marker%d.png",siteCount[idx];  // As per @anna's comment I am not sure how you are managing this -  It would be better if the number came from the associated annotation object  
        annotationView.image = [UIImage imageNamed:str];
    }

    annotationView.annotation = annotation;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM