1

我打電話服務,並返回了一堆的緯度和經度這我然後使用MapKit在地圖上放置的。MKAnnotationView RightCallOut按鈕崩潰我的應用程序,當我點擊它

使用MKAnnotationView我加入一個RightCallOutButton到每個註釋。因此我不得不創建一個新的MapDelegate。下面的代碼。

如果我點擊我創建應用程序崩潰的按鈕,我從MonoTouch的話說,選擇得到一個錯誤是accings omething這已是GC'd(垃圾回收)。

所以我的問題是,我應該在哪裏設置RightCalloutAccessoryView和我應該在哪裏創建按鈕,如果不是在下面這段代碼?

public class MapDelegage : MKMapViewDelegate { 

    protected string _annotationIdentifier = "BasicAnnotation"; 
    public override MKAnnotationView GetViewForAnnotation (MKMapView mapView,       NSObject annotation) { 

MKAnnotationView annotationView = mapView.DequeueReusableAnnotation(this._annotationIdentifier); 


if(annotationView == null) { 
    annotationView = new MKPinAnnotationView(annotation, this._annotationIdentifier); 
} else { 
    annotationView.Annotation = annotation; 
} 


annotationView.CanShowCallout = true; 
(annotationView as MKPinAnnotationView).AnimatesDrop = true;  
(annotationView as MKPinAnnotationView).PinColor = MKPinAnnotationColor.Green; 
annotationView.Selected = true;  
var button = UIButton.FromType(UIButtonType.DetailDisclosure); 
button.TouchUpInside += (sender, e) => { 
new UIAlertView("Testing", "Testing Message", null, "Close", null).Show(); 
} ; 

annotationView.RightCalloutAccessoryView = button; 
return annotationView; 
} 

} 

回答

1
annotationView = new MKPinAnnotationView(annotation, this._annotationIdentifier); 
... 
var button = UIButton.FromType(UIButtonType.DetailDisclosure); 

你應該避免聲明局部變量來保存你期望活得比方法本身的引用。一旦有到annotationViewbutton沒有提到垃圾收集器(GC)是免費收集他們(管理部分),即使它是本地同行仍然存在。然而,當他們回撥給他們,你會得到一個崩潰。

最簡單的解決方案是在銷燬視圖時保留它們的列表(在課程級別,即List<MKPinAnnotationView>字段)清除列表。 UIButton應該沒有必要,因爲視圖和它之間有一個參考。

注:工作正在做隱藏這在MonoTouch中的未來版本開發的複雜性。可悲的是,目前你不能忽視這些問題。

+0

所以我會在我的MapDelegate類中創建一個List <>?當我摧毀視圖時,不清楚你的意思。我在哪裏添加PInAnnotations?我以爲GetViewForAnnotation每次只處理一個註釋? –

+0

好吧,我想我明白了,而且我明白了。我創建了列表並將annoationView添加到列表中。我是否需要擔心我創建的列表或將在某些時候被GC破壞的列表? –

+0

一旦這個領域的父實例沒有更多的引用,它將由GC完成,但是由於它可能會變大,所以最好儘快確保'父'被丟棄。如果您隱藏/顯示視圖(或保留緩存並僅在您收到來自iOS的低內存警告時將其清除),則可能還需要手動清除它。 – poupou

相關問題