在NSThread上调用时,MKMapView无法加载? [英] MKMapView Not Loading When Called on an NSThread?
问题描述
我正在用一种名为"generateMap"的方法创建一个MKMapView.在viewDidLoad内部,这有效:
I am creating a MKMapView in a method named "generateMap". From inside viewDidLoad, this works:
[self generateMap];
但这会导致地图快速加载然后消失,仅留下空白的灰色网格:
but this causes the map to quickly load and then disappear, leaving only the blank grey grid:
[NSThread detachNewThreadSelector:@selector(generateMap) toTarget:self withObject:nil];
有什么想法为什么当我通过线程调用该方法时会发生这种情况?
Any ideas why this might be happening when I call the method through a thread?
我最终这样做:
-(void)viewDidLoad {
[NSThread detachNewThreadSelector:@selector(spinTheSpinner) toTarget:self withObject:nil];
[self performSelectorOnMainThread:@selector(generateMap) withObject:nil waitUntilDone:NO];
[super viewDidLoad];
}
这使我可以使用微调器(UIActivityIndicator)并根据需要加载MKMapView.我设置为"waitUntilDone:No]",以便在使用generateMap完成操作之前,屏幕切换MapView.否则,直到generateMap完成,我们才会看到微调器,并且只会看到空白屏幕.
This allows me to have a spinner (UIActivityIndicator) and load the MKMapView as I want. I set "waitUntilDone:No]" so that the screen switches the MapView before it is done with generateMap. Otherwise, we would not see the spinner and would only see a blank screen until generateMap was done.
推荐答案
我不建议尝试通过主线程以外的任何方式更新UI.
I don't think it is advisable to try to update the UI through anything but the main thread.
当您尝试使用-performSelectorOnMainThread:withObject:waitUntilDone:
在主线程上运行-generateMap
时会发生什么?
What happens when you try to run -generateMap
on the main thread with -performSelectorOnMainThread:withObject:waitUntilDone:
?
例如:
[self performSelectorOnMainThread:@selector(generateMap) withObject:nil waitUntilDone:YES];
这篇关于在NSThread上调用时,MKMapView无法加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!