弹出视图控制器时,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];
}