【问题标题】:how to use MBProgressHUD to pause the screen when the loading animating加载动画时如何使用 MBProgressHUD 暂停屏幕
【发布时间】:2016-11-12 09:35:46
【问题描述】:

我有一个问题。
我在 tableviewcontroller 中使用 MBProgressHUD。
我希望 MBProgressHUD 加载动画然后用户不能触摸任何东西并等待服务器数据显示在 tableview 上。
我想暂停屏幕直到 hud hideAnimated。

谢谢!

这是我的代码:

MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.listTableView animated:YES];
hud.label.text = @"Loading";
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 0.01 * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
    [self getInfo:nil];
});
hud.userInteractionEnabled = YES;
[hud hideAnimated:YES afterDelay:2];

【问题讨论】:

  • 你有请求的回调吗?你应该在那里做[hud hideAnimated:YES]
  • 你想在哪里暂停???

标签: ios iphone uitableview mbprogresshud


【解决方案1】:

在这种情况下,您不应使用 [hud hideAnimated:YES afterDelay:2]dispatch_after 也没有任何意义。

问题是您的请求可能会很快完成(例如 0.1 秒),并且 hud 仍将显示另外 1.9 秒。或者它可能会等待很长时间,例如 30 秒或您设置的任何超时,因此您的代码会在实际显示时隐藏 hud。

您应该做的是在请求完成(成功与否)时调用完成块(或委托方法)。

MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.label.text = @"Loading";
[self getInfoWithSuccess:^{
    [hud hideAnimated:YES];
    //your code here        
} failure:^(NSError *error) {
    [hud hideAnimated:YES];
    //show error message if needed
}];

请求方法本身:

- (void) getInfoWithSuccess: (void(^)()) success failure: (void(^)(NSError *)) failure {
    //do async request here and call success or failure block when it's finished
}

【讨论】:

    猜你喜欢
    • 2011-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多