iphone - 当键值观察者仍然注册时释放(反向地理编码器)

标签 iphone reverse-geocoding mkannotationview

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

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];

我曾尝试在关闭 View 之前将 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;
}

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

最佳答案

您可能在此处遇到多个问题。对于您设置的每个委托(delegate),您应该在 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 中)并确保撤消您在其中所做的所有操作。特别是任何触发回调的东西。

关于iphone - 当键值观察者仍然注册时释放(反向地理编码器),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5071912/

相关文章:

iphone - iPhone 上的 TRACE 日志记录

iphone - 将主 NSManagedObjectContext 替换为后台线程中的 MOC,而不是合并

ios - CLLocation 列表中的地点/城市名称

ios - MapKit uber风格 map 中心标注

iphone - MKAnnotationView 从 MKMapView 窃取触摸

iphone - 从 super View 中删除当前 View

iphone - 如何在不从 map 中添加/删除的情况下移动 MKAnnotation?

android - Android 中获取用户当前所在城市

geocoding - 对城市的区域名称进行地理编码以获取经度和经度

ios - 选择注释 View 时指定方法