2012-06-13 35 views
9

刪除註釋:的MKMapView - 刪除註釋導致應用程序崩潰從下面的方式我的地圖視圖

if ([[self.mapView annotations] count] > 0) 
{ 
    [self.mapView removeAnnotations:[self.mapView annotations]]; 
} 

會導致我的應用程序有以下例外崩潰:

*** Terminating app due to uncaught exception 'NSRangeException', reason: 'Cannot remove an observer <MKAnnotationContainerView 0xe87b420> for the key path "title" from <PFAnnotation 0x10851230> because it is not registered as an observer.' 

的註釋中加入採用以下方式:

CLLocationCoordinate2D pinPosition; 
for (int index = 0; index < [array count]; index++) 
{   
    Station *aStation = [array objectAtIndex:index]; 
    PFAnnotation *stationPin = [[PFAnnotation alloc] init]; //StationPinView 
    pinPosition = CLLocationCoordinate2DMake([[aStation valueForKey:@"latitude"] doubleValue], [[aStation valueForKey:@"longitude"] doubleValue]); 
    stationPin.stationName = [aStation valueForKey:@"stationName"]; 
    stationPin.stationPosition = pinPosition; 
    stationPin.stationLength = [aStation valueForKey:@"platformLength"]; 

    [self.mapView addAnnotation:stationPin]; 
    [stationPin release];   


} 

我的PFAnnotation.h是:

@interface PFAnnotation : NSObject <MKAnnotation> 
{ 
    NSString *stationName; 
    CLLocationCoordinate2D stationPosition; 
    NSNumber *stationLength; 

} 

@property (nonatomic, retain) NSString *stationName; 
@property CLLocationCoordinate2D stationPosition; 
@property (nonatomic, retain) NSNumber *stationLength; 


@end 

和我PFAnnotation.m是:

@implementation PFAnnotation 

@synthesize stationName; 
@synthesize stationPosition; 
@synthesize stationLength; 


- (CLLocationCoordinate2D)coordinate; 
{ 
    return stationPosition; 
} 

- (NSString *)title 
{ 
    return stationName; 

} 

- (NSString *)subtitle 
{ 
    if (stationLength == nil) 
     return nil; 
    else 
     return [NSString stringWithFormat:@"Platform Length: %@ft",stationLength]; 
} 


- (void)dealloc { 
    [stationName release]; 
    [stationLength release]; 
    [super dealloc]; 
} 

我已經在一些其他的線程,從後臺線程設置註釋性是上述錯誤的原因讀取。但就我而言,並非如此,因爲每一件事都在主線上進行。請指教。

+0

請首先添加一些代碼,以便如何添加註釋。謝謝! –

+0

@Guntis Treulands我編輯了問題並添加了代碼。請參見。 – DroidHeaven

+0

當你不設置stationName和stationLength時會發生什麼?它仍然崩潰? –

回答

4

ok ..終於解決了!!!我認爲這是由於在添加註釋期間提供的動畫。由於有多個註釋與動畫背靠背添加,並且註釋在動畫開始之前被移除,因此可能會有對已發佈註釋的引用(這是我的猜測)。此外,刪除+添加過程是在每個regionDidChangeAnimated調用上進行的,這可能會在刪除和添加過程之間產生重疊。無論如何,我是如何解決這個問題的,我提供了一個計時器,它在每個regionDidchangeAnimated後1秒後纔會觸發,以確保用戶完成拖動。因此避免了不必要的附加和刪除註釋,我可以避免崩潰。感謝所有人在這裏花時間支持我,特別是Guntis Treulands。

1

在你的PFAnnotation類中,你是否聲明瞭標題和字幕屬性,因爲它們在協議中?

http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKAnnotation_Protocol/Reference/Reference.html

+0

我已經在問題中添加了類文件。它們是否符合Apple的要求? (我不是專家開發者,請原諒我)。 – DroidHeaven

+0

我不明白你爲什麼重寫標題/副標題的吸引人......你應該聲明屬性併合成它們;後來當你創建註釋只是設置他們 – Valerio

+0

因爲我是維護人員,我不知道爲什麼原始開發人員這樣寫:)。讓我試試你指定的路線。 – DroidHeaven

1

如果你PFAnnotation真的有字符串值不正確制定者干將:

從這裏:http://cocoadevcentral.com/d/learn_objectivec/

二傳手:

- (void) setCaption: (NSString*)input 
{ 
    [caption autorelease]; 
    caption = [input retain]; 
} 

消氣:

- (NSString*) caption 
{ 
    return caption; 
} 

發佈:

- (void) dealloc 
{ 
    [caption release]; 
    [super dealloc]; 
} 

也 - 這是更方便地提供這樣的座標:(也適用於iOS 3.1.3)

stationPin.stationPosition = (CLLocationCoordinate2D) {[[aStation valueForKey:@"latitude"] doubleValue], [[aStation valueForKey:@"longitude"] doubleValue]} 

比(僅iOS 4的)

stationPin.stationPosition = CLLocationCoordinate2DMake([[aStation valueForKey:@"latitude"] doubleValue], [[aStation valueForKey:@"longitude"] doubleValue]); 
+0

yeah..but似乎是不會幫助任何:(。 – DroidHeaven

+1

你可以嘗試有可能實現這些mapPin類文件? http://stackoverflow.com/questions/8180513/removing-adding-annotations-to-mapview - 因爲內存泄漏/ 8180605#8180605 希望可以解決這個問題。 –

+0

謝謝:) ..讓我試試 – DroidHeaven

1

請檢查是否在代碼中的任何位置完成了對屬性「title」的觀察者的顯式移除。

+0

讓我檢查一下。 – DroidHeaven

+1

,並且還請通過顯式調用「performSelectorOnMainthread」在主線程中強制刪除和添加註釋。 – iosDev

相關問題