17

当我的视线消失时,我收到以下消息:

An instance 0x1c11e0 of class MKAnnotationView 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. Set a breakpoint on NSKVODeallocateBreak to stop here in the debugger. Here's the current observation info:

(上下文:0x0,属性:0x1e98d0>)

定义和启动反向地理编码的代码是:

geo=[[MKReverseGeocoder alloc] initWithCoordinate:droppedAt];
        geo.delegate=self;
        [geo start];

在我关闭视图之前,我尝试将 geo.delegate 设置为 nil。那太简单了。我也试过:

for (id <MKAnnotation> annotation in mvMap.annotations) {
    [[mvMap viewForAnnotation:annotation] removeObserver:self forKeyPath:@"selected"];
}

这会引发一个错误,上面写着:

*由于未捕获的异常“NSRangeException”而终止应用程序,原因:“无法删除“选择”的关键路径的观察者,因为它没有注册为观察者。

我对注释代码的看法是:

-(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
    MKAnnotationView *aView;

    aView=(MKAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier:annotation.title];
    if (aView==nil) 
        aView=[[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotation.title] autorelease];
    else
        aView.annotation=annotation;
    [aView setImage:[UIImage imageNamed:selIcon]];
    aView.canShowCallout=TRUE;
    aView.draggable=YES;
    return aView;
}

我在旋转时在这里按下按钮和翻转开关。知道我可以在这里做什么吗?

4

2 回答 2

13

你可能有多个问题,在这里。对于您设置的每个委托,您应该在 dealloc 时将其清除。对于您设置的每个观察者,您应该清除它,与通知等相同。

所以你的 dealloc 应该有(在网络浏览器中输入,你可能需要调整):

- (void) dealloc
{
     [[NSNotificationCenter defaultCenter] removeObserver: self];
     for (id <MKAnnotation> annotation in mvMap.annotations)
     {
          // remove all observers, delegates and other outward pointers.
          [[mvMap viewForAnnotation:annotation] removeObserver: self forKeyPath: path];
     }

     gel.delegate = nil;
     // etc., your other stuff
     [super dealloc];  // Only in non-ARC legacy code.  Modern stuff skips this.
}

完成您的设置(可能在 viewDidLoad 中)并确保取消您在其中所做的所有操作。特别是任何触发回调的东西。

于 2011-03-30T15:53:17.867 回答
2

可拖动的别针也有同样的麻烦。我发现以下代码可以解决它们:

(void)mapView:(MKMapView *)theMapView annotationView:(MKAnnotationView *)annotationView didChangeDragState:(MKAnnotationViewDragState)newState fromOldState:(MKAnnotationViewDragState)oldState {

if (newState == MKAnnotationViewDragStateStarting) {

    // some my code

} else if (newState == MKAnnotationViewDragStateCanceling) {

    [annotationView removeObserver:theMapView forKeyPath:@"dragState"];

} else if (newState == MKAnnotationViewDragStateEnding) {

    [annotationView removeObserver:theMapView forKeyPath:@"dragState"];
}
}
于 2011-10-28T11:59:24.680 回答