调用addOverlay时已取消分配MKMapView [英] MKMapView dealloc'ed when calling addOverlay

查看:173
本文介绍了调用addOverlay时已取消分配MKMapView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在- (void)viewDidLoad

NSString *template = @"http://tile.openstreetmap.org/{z}/{x}/{y}.png";
self.onlineOverlay = [[MKTileOverlay alloc] initWithURLTemplate:template];
self.onlineOverlay.canReplaceMapContent = YES;
[_mapView addOverlay:self.onlineOverlay level:MKOverlayLevelAboveLabels];

日志为:(启用了僵尸对象)

The log is:(with zombie objects enabled)

*** -[VKRasterOverlayTileSource invalidateRect:level:]: message sent to deallocated instance 0xf1863e0

仅当我过早关闭包含地图视图的视图控制器时,才会发生这种情况. 如果我等待几秒钟,它不会崩溃. 通过从导航控制器中弹出视图控制器,可以关闭该视图控制器,

It only happens when I dismiss the view controller containing the map view too soon. If I wait a few seconds it doesn't crash. The view controller gets dismissed by pop'ing it from the navigation controller,

我尝试了几种选择:

  1. mapView&在- (void) dealloc and in - (void)viewDidDisappear
  2. 中将mapView.delegate设置为nil
  3. BOOL finishedLoading,该视图在加载视图时设置为FALSE,在调用地图视图委托方法- (void)mapViewDidFinishRenderingMap:(MKMapView *)mapView fullyRendered:(BOOL)fullyRendered时设置为TRUE.
  4. - (void)mapView:(MKMapView *)mapView didAddOverlayRenderers:(NSArray *)renderers似乎在该方法完成之前被调用.
  1. mapView & mapView.delegate is set to nil in - (void) dealloc and in - (void)viewDidDisappear
  2. BOOL finishedLoading that is set to FALSE when the view loads and set to TRUE when the map view delegate method - (void)mapViewDidFinishRenderingMap:(MKMapView *)mapView fullyRendered:(BOOL)fullyRendered is called.
  3. - (void)mapView:(MKMapView *)mapView didAddOverlayRenderers:(NSArray *)renderers seems to be called before the method is done.

如何检查覆盖层是否已完全添加?

How can I check if the overlay has been fully added?

或者如果视图控制器被关闭,是否可以取消覆盖?

Or is it possible to cancel the overlay if the view controller gets dismissed?

视图控制器位于导航控制器堆栈中,并显示并弹出. 我自己没有使用任何线程.从工具中,我可以看到还有另一个线程,所以我猜是addOverlay:正在某个地方创建线程.

The view controller is in a navigation controller stack and is presented and popped. I'm not using any threads myself. From instruments I can see that there is another thread, so my guess is that addOverlay: is creating a thread somewhere.

mapView是viewController的属性,也是委托.

The mapView is a property of the viewController and is the delegate as well.

推荐答案

在tileOverlay子类中设置指向mapView的指针.这样,当调用结果块时,mapView仍处于活动状态.您将必须在tileOverlay子类中手动添加mapView属性.

Set a pointer to your mapView in the tileOverlay subclass. This way the mapView is still alive when the result block is called. You will have to manually add the mapView property in your tileOverlay subclass.

NSString *template = @"http://tile.openstreetmap.org/{z}/{x}/{y}.png"; self.onlineOverlay = [[MKTileOverlay alloc] initWithURLTemplate:template]; self.onlineOverlay.canReplaceMapContent = YES; self.onlineOverlay.mapView = _mapView; [_mapView addOverlay:self.onlineOverlay level:MKOverlayLevelAboveLabels];

NSString *template = @"http://tile.openstreetmap.org/{z}/{x}/{y}.png"; self.onlineOverlay = [[MKTileOverlay alloc] initWithURLTemplate:template]; self.onlineOverlay.canReplaceMapContent = YES; self.onlineOverlay.mapView = _mapView; [_mapView addOverlay:self.onlineOverlay level:MKOverlayLevelAboveLabels];

这篇关于调用addOverlay时已取消分配MKMapView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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