弹出视图控制器时,MKMapView使应用程序崩溃 [英] MKMapView crashes app when view controller popped

查看:83
本文介绍了弹出视图控制器时,MKMapView使应用程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有MKMapView的视图控制器,该控制器调用

I have a view controller with an MKMapView that calls

[self.mapView setRegion:region animated:YES]; 

将地图从A重新定位到B.

which repositions the map from A to B.

将保存MKMapView的视图控制器设置为委托,并在

The view controller which holds the MKMapView is set as the delegate and in

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated

我有一些代码将触发另一个setRegion:animated:到MKMapView,以便地图将自动放大到新位置.

I have some code that will trigger another setRegion:animated: to the MKMapView so that the map will zoom in on the new position automatically.

如果我popViewControllerAnimated,一切都可以正常工作:MKMapView动画平移和缩放后,视图控制器.

Everything works fine if I popViewControllerAnimated: the view controller AFTER the MKMapView animation is done panning and zooming.

但是,当我尝试popViewControllerAnimated时:当前的视图控制器,当MKMapView运行其动画时,应用程序崩溃,并显示消息发送到已释放的实例".

However, when I try to popViewControllerAnimated: the current view controller WHILE the MKMapView is running it's animation, the app crashes with "message sent to deallocated instance".

从调试器的外观来看,我认为MKMapView试图从弹出并释放的委托中调用方法.

From the looks of the debugger, I think that MKMapView is trying to call a method from a popped and deallocated delegate.

所以我尝试了

[self.mapView setDelegate:nil];
self.mapView = nil;

在viewDidUnload中没有运气.该应用仍然崩溃.

in viewDidUnload with no luck. The app still crashes consistently.

我唯一想到的就是创建一个单独的新委托类,并从父视图控制器保留该类,以便即使在释放包含它的视图控制器之后,MKMapView也会有一个要调用的委托.

The only thing I could think of was to create a separate new delegate class and retain that class from the parent view controller so that the MKMapView would have a delegate to call even after the view controller that contains it is deallocated.

为什么会这样? 还有其他干净"的选择吗?

Why is this happening? Are there any other "clean" options?

推荐答案

一个朋友帮助我得到了这个.

A friend helped me get this one.

我实现了自己的用于弹出视图控制器的方法,而不是使用默认导航控制器的后退按钮.我只需要添加[self.mapView setDelegate:nil];在弹出视图控制器之前.

I implemented my own method for popping the view controller instead of using the default navigation controller's back button. I just had to add [self.mapView setDelegate:nil]; before I popped the view controller.

- (void)goBack
{
    [self.mapView setDelegate:nil];
    [self.navigationController popViewControllerAnimated:YES];
}

这篇关于弹出视图控制器时,MKMapView使应用程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆