2013-01-24 25 views
1

UICalloutView willRemoveSubview:]:發送到釋放實例的消息;標註按鈕崩潰的應用程序 - MapKit

只有當我點擊標註按鈕時,纔會發生這種情況,但不是第一次,也不是第2次,而是第3次。所以我點擊自定義AnnotationView,標註彈出,這很好。我再次點擊它,標註彈出,都很好。我點擊另一個,轟然消失。它只會發生,如果設置正確的accesoryview是一個按鈕。

要記住的一個關鍵方面..只發生在iOS 6 ...(去圖)。

我真的被困在這一個 - 一些幫助,將不勝感激。

if ([annotation isKindOfClass:[RE_Annotation class]]) 
{ 
    RE_Annotation *myAnnotation = (RE_Annotation *)annotation; 

    static NSString *annotationIdentifier = @"annotationIdentifier"; 

    RE_AnnotationView *newAnnotationView = (RE_AnnotationView *)[mapViews dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier]; 


    if(newAnnotationView) 
    { 
     newAnnotationView.annotation = myAnnotation; 
    } 
    else 
    { 
     newAnnotationView = [[RE_AnnotationView alloc] initWithAnnotation:myAnnotation reuseIdentifier:annotationIdentifier]; 
    } 

    return newAnnotationView; 
} 
return nil; 

此外,這是我的initwithannotation方法:

- (id)initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier 
{ 

    self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier]; 
    if(self) 
    { 

    RE_Annotation *myAnnotation = annotation; 

    self = [super initWithAnnotation:myAnnotation reuseIdentifier:reuseIdentifier]; 

    self.frame = CGRectMake(0, 0, kWidth, kHeight); 

    self.backgroundColor = [UIColor clearColor]; 

    annotationView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"map_pin_pink.png"]]; 

    annotationView.frame = CGRectMake(0, 0, kWidth - 2 *kBorder, kHeight - 2 * kBorder); 



    [self addSubview:annotationView]; 

    [annotationView setContentMode:UIViewContentModeScaleAspectFill]; 

    self.canShowCallout = YES; 

    self.rightCalloutAccessoryView = [[UIButton buttonWithType:UIButtonTypeInfoLight] retain]; ///if i take it out it doesnt crash the app. if i leave it it says that message 
    } 

    return self ; 

} 
+0

你能告訴我們你正在使用的代碼嗎?沒有看到你在做什麼是不可能的。 –

+0

我的代碼太長以至於不適合:( – user1878326

+0

)不要發佈所有代碼,只是相關部分,例如處理地圖註釋的代碼。 –

回答

1

initWithAnnotation方法,有這樣一行:

self.rightCalloutAccessoryView = 
    [[UIButton buttonWithType:UIButtonTypeInfoLight] retain]; 
///if i take it out it doesnt crash the app. if i leave it it says that message 

通過 「它」,你必須指retain這意味着該應用程序不使用ARC。


此基礎上,你應該做如下更正:

  • viewForAnnotation,你需要在你的Alloc +初始化它,否則有泄露到autorelease的觀點:

    newAnnotationView = [[[RE_AnnotationView alloc] 
        initWithAnnotation:myAnnotation 
        reuseIdentifier:annotationIdentifier] autorelease]; 
    
  • initWithAnnotation中,創建標註按鈕時刪除retain,因爲buttonWithType返回自動釋放的對象(你不想過度保留它):

    self.rightCalloutAccessoryView = 
        [UIButton buttonWithType:UIButtonTypeInfoLight]; 
    


另一個可能毫無關聯的問題是,在initWithAnnotation,代碼是調用super initWithAnnotation兩次。這看起來沒有必要,可能有害。刪除第二個電話。


上述變化至少解決了顯示的代碼問題。但是,在其他應用程序中可能還有其他類似的與內存管理相關的問題,可能會導致崩潰。檢查並解決分析儀報告的所有問題。

關於「它只發生在iOS 6」的事實:iOS 6可能不太容忍內存管理錯誤,或者SDK中的內部更改可能會在早期暴露或顯示這些錯誤。無論如何,上述修復是必要的。


一個不相關的一點是,應該沒有必要手動創建一個UIImageViewaddSubview它的註解視圖。您可以設置註釋視圖的image屬性。

+0

好吧。很多反饋,我嘗試與annotationview圖像屬性和idd的init 2次是一個不好的做法,實際上是一個錯字...實際上我忘了發佈註釋,我會做出適當的改變並告訴你...相當誠實我懷疑他們會改變一些東西,但是非常感謝你的時間,我會回來的 – user1878326

+0

懷疑......似乎沒有任何效果。 – user1878326

相關問題