当视图控制器弹出时 MKMapView 使应用程序崩溃
我有一个带有 MKMapView 的视图控制器,它调用
[self.mapView setRegion:region animated:YES];
它将地图从 A 重新定位到 B。
保存 MKMapView 的视图控制器被设置为委托,并且
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
我有一些代码将触发另一个 setRegion:animated: 到 MKMapView ,以便地图将自动放大新位置。
如果我 popViewControllerAnimated: MKMapView 动画完成平移和缩放后的视图控制器,则一切正常。
但是,当我尝试 popViewControllerAnimated: 当前视图控制器而 MKMapView 正在运行它的动画时,应用程序崩溃并显示“消息发送到已释放的实例”。
从调试器的外观来看,我认为 MKMapView 正在尝试从弹出和释放的委托中调用方法。
所以我尝试了
[self.mapView setDelegate:nil];
self.mapView = nil;
viewDidUnload 但没有成功。该应用程序仍然不断崩溃。
我唯一能想到的就是创建一个单独的新委托类并从父视图控制器中保留该类,以便 MKMapView 即使在包含它的视图控制器被释放后也将有一个委托可以调用。
为什么会发生这种情况? 还有其他“干净”的选择吗?
I have a view controller with an MKMapView that calls
[self.mapView setRegion:region animated:YES];
which repositions the map from A to B.
The view controller which holds the MKMapView is set as the delegate and in
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
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.
Everything works fine if I popViewControllerAnimated: the view controller AFTER the MKMapView animation is done panning and zooming.
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".
From the looks of the debugger, I think that MKMapView is trying to call a method from a popped and deallocated delegate.
So I tried
[self.mapView setDelegate:nil];
self.mapView = nil;
in viewDidUnload with no luck. The app still crashes consistently.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
一位朋友帮我得到了这个。
我实现了自己的弹出视图控制器的方法,而不是使用默认导航控制器的后退按钮。我只需添加 [self.mapView setDelegate:nil];在我弹出视图控制器之前。
A friend helped me get this one.
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.
好吧,这才是真正的答案。它来自 Apple 文档,但 MKMapView 中缺少它。它只能在其委托协议的文档中找到:
“在释放已设置委托的 MKMapView 对象之前,请记住将该对象的委托属性设置为 nil。您可以执行此操作的一个地方是在您处置的 dealloc 方法中地图视图。”
注意:这也适用于 UIWebView。
我在委托的 dealloc 方法中将 MapView 的委托指针设置为 nil,我们的崩溃似乎已被消除。
OK, this is the real answer. It's from the Apple doc, but it's missing from MKMapView. It's only found under the documentation for its delegate protocol:
"Before releasing an MKMapView object for which you have set a delegate, remember to set that object’s delegate property to nil. One place you can do this is in the dealloc method where you dispose of the map view."
NOTE: This also applies to UIWebView.
I set the MapView's delegate pointer to nil in the delegate's dealloc method, and our crashes seem to have been eliminated.
我的问题没有通过在我的视图控制器中将 MKMapView 的委托设置为 nil 来解决
我必须在 RootViewController 中对包含 MKMapView 的 UIViewController 进行 __strong 引用。
My problem was not solved by setting delegate of MKMapView to nil in my view Controller
I had to make a __strong reference of my UIViewController containing MKMapView in my RootViewController.
我已经完成了聚类,并标记了选择的注释,如下所示
mapView.selectAnnotation(annotation,animated: true)
。弹出时,
deinit
方法曾经崩溃。因此,按下后退按钮后,我只需添加此行
mapView.deselectAnnotation(selectedAnnotation,animated: false)
即可解决崩溃问题。I had done Clustering and was marking the Annotation selected like so
mapView.selectAnnotation(annotation, animated: true)
.while popping, the
deinit
method used to crash.So on press of back button I just add this line
mapView.deselectAnnotation(selectedAnnotation, animated: false)
and it solved the crash.以下代码可能会解决您的问题:
The following code is likely to asolve your problem: