您的位置:首页 > 移动开发 > IOS开发

IOS MBProgressHUD 无法显示的问题

2016-11-14 17:53 260 查看
最近在项目里碰到了一个问题:

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

});
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios