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 には以下が必要です (Web ブラウザーで入力すると、調整が必要になる場合があります)。

- (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 に答える