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

相关问题