0

我有一个使用 MapView 的应用程序,并且我有 4 种类型的 MKAnnotation。我在屏幕上也有 4 个按钮。每个按钮都应该显示或隐藏一种 MKannotation 类型。我能够跟踪类型并删除它们。但是当我尝试添加任何 MKannotation 时,我收到一条错误消息。经过搜索,我发现了一个类似的问题,但尚未得到解答。

An instance 0x6ec5750 of class MapPoint was deallocated while key value observers were    still registered with it. Observation info was leaked, and may even become mistakenly attached  to some other object.

ios5从地图视图中删除注释

首先,我在调用 Web 服务后添加 MKAnnotation:

for (int x=0; x<PromotionsArray.count; x++) 
{
    Promotion *pr = [PromotionsArray objectAtIndex:x];
    CLLocationCoordinate2D newCoord ={ pr.promotionLatitude, pr.promotionLongitude};
    MapPoint *mp= [[MapPoint alloc] initWithCoordinate:newCoord];
    mp.currentTitle=pr.PromotionTitle;
    mp.currentSubTitle=pr.RetailerName;
    mp.currentPromotionArrayID=[NSString stringWithFormat:@"%d",x];
    mp.currentRetailerID = pr.RetailerID;
    mp.currentPromotionType = pr.PromotionType;
    [mapView addAnnotation:mp];
}

现在我在人视图上有 4 个按钮。Type1、Type2、Type3 和 Type4 按钮。如果我单击 Type1 按钮,它将使用以下完美运行的代码删除 Type1 的所有 MKAnnotation:

for (id <MKAnnotation>  myAnnot in [mapView annotations])
    {
        if (![myAnnot isKindOfClass:[MKUserLocation class]])
        {
            if([(MapPoint *)myAnnot currentPromotionType]==PromotionType1)
            {
                NSLog(@"Hiding All Type1 Annotations");
                [mapView removeAnnotation:myAnnot];
            }
        }
    }

现在,如果我想再次显示 Type1,我使用以下导致问题的代码:

    for (int x=0; x<PromotionsArray.count; x++) 
    {
        Promotion *pr = [PromotionsArray objectAtIndex:x];
        if (pr.promotionType==PromotionType1) 
        {
            CLLocationCoordinate2D newCoord ={ [pr.promotionLatitude doubleValue],[pr.promotionLongitude doubleValue]};
            MapPoint *map= [[MapPoint alloc] initWithCoordinate:newCoord];
            map.currentTitle=pr.PromotionTitle;
            map.currentSubTitle=pr.RetailerName;
            map.currentPromotionArrayID=[NSString stringWithFormat:@"%d",x];
            [mapView addAnnotation:map];
        }
    }

问题出现在这一行 [mapView addAnnotation:map]; 这导致了我提到的错误消息 relier(又是这里)

An instance 0x6ec5750 of class MapPoint was deallocated while key value observers were    still registered with it. Observation info was leaked, and may even become mistakenly attached  to some other object.
4

2 回答 2

0

现在工作,问题完全是另一种方法。我试图通过这样做将双精度值转换为 NSString:

[Nsstring stringWithFormat:@"%d",doubleVal]; 

%d 应该是 %f !!!! 这是一个愚蠢的小错误。感谢所有人的帮助和@AnnaKarenina 的提示

于 2012-05-11T08:00:47.920 回答
0
// REMOVING ALL ANNOTATION
    for (id <MKAnnotation>  myAnnot in [objMapView annotations])
    {
        if (![myAnnot isKindOfClass:[MKUserLocation class]])
        {
            [objMapView removeAnnotation:myAnnot];
        }
    }
于 2014-10-09T10:09:47.303 回答