我有一个使用 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.
首先,我在调用 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.