当我单击 MKAnnotationView RightCallOut 按钮时,它会使我的应用程序崩溃

发布于 2024-12-13 02:31:24 字数 1321 浏览 2 评论 0原文

我正在调用一个服务并返回一堆纬度和经度,然后使用 MapKit 将其放置在地图上。

使用 MKAnnotationView 我向每个注释添加 RightCallOutButton。

所以我必须创建一个新的 MapDelegate。代码如下。

如果我单击该按钮,我会导致应用程序崩溃,并且从 MonoTouch 收到错误消息,指出选择器正在添加已被 GC 处理的内容(垃圾收集)。

所以我的问题是,如果不在下面的代码中,我应该在哪里设置 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;
}

}

I'm calling a service and returning a bunch of latitudes and longitudes which I'm then placing on a map using MapKit.

using MKAnnotationView I'm adding a RightCallOutButton to each annotation.

So I had to create a new MapDelegate. Code below.

If I click on the button I create the app crashes and I get an error from MonoTouch saying the selector is accings omething that has already been GC'd (garbage collected).

So my question would be, where should I set the RightCalloutAccessoryView and where should I create the button, if not in this code below?

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;
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无妨# 2024-12-20 02:31:24
annotationView = new MKPinAnnotationView(annotation, this._annotationIdentifier);
...
var button = UIButton.FromType(UIButtonType.DetailDisclosure);

您应该避免声明局部变量来保存您希望比方法本身寿命更长的引用。一旦没有对 annotationViewbutton 的引用,垃圾收集器 (GC) 就可以自由地收集它们(托管部分),即使它是本机对应部分仍然存在。但是,当调用它们的回调时,您会崩溃。

最简单的解决方案是保留它们的列表,并在销毁视图时(在类级别,即 List 字段)清除列表。 UIButton 应该不是必需的,因为视图和它之间存在引用。

注意:正在努力在 MonoTouch 的未来版本中向开发人员隐藏这种复杂性。遗憾的是,你现在不能忽视这些问题。

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

You should avoid declaring local variables to hold references you expect to outlive the method itself. Once there's no reference to annotationView or button the Garbage Collector (GC) is free to collect them (the managed part) even if it's native counterparts still exists. However when a callback to them is called you'll get a crash.

The easiest solution is to keep a list of them and (at the class level, i.e. a List<MKPinAnnotationView> field) clear the list when you destroy the view. The UIButton should not be necessary since there's a reference between the view and it.

NOTE: work is being done to hide this complexity from developers in future versions of MonoTouch. Sadly you cannot ignore such issues at the moment.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文