最近在项目里碰到了一个问题:
MBProgressHUD无法显示出来(之前在项目里也用过这个,没出现过问题)
东找西找找到下面的解决方案:
1、(“MBProgressHUD needs to be accessed on the main thread.”)报错 很明显意思是需要跑在主线程上
dispatch_async(dispatch_get_main_queue(), ^{
//获取主线程
});
2、MBProgressHUD 的HUD不能立刻显示出来(在出现问题后我有试过将MBProgressHUD后面的代码都删了 – 结果是可以正常显示的,只是没有想到这一点“UIKit 不能在当前run loop结束前重画,即需要在下一个run loop 周期才能重画,更新UI。”)
使用 MBProgressHUD,如果在一个函数中添加了 HUD,又在函数结束前做了耗时操作,此时hud 不会立刻显示出来,而是需要等到函数结束后才能显示:
- (void)showSomething {
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
hud.mode = MBProgressHUDModeText;
hud.labelText = @"Some message...";
hud.margin = 10.f;
hud.removeFromSuperViewOnHide = YES;
for (int i=0; i<5; i++) {
//
}
}
解决方案:
A、你下面方法运行你的耗时程序,然后在myTadk结束时隐藏 HUD
// Setup and show HUD here
[self performSelector:@selector(myTask) withObject:nil afterDelay:0.001];(没看懂)
B、你可以手动的运行 run loop(这是我使用的方法 有效)
// Setup and show HUD here
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate distantPast]];
C、使用blocks
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.001 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void){
// Insert myTask code here
});