我正在玩弄 MBProgressHUD。 viewcontroller(tableview)中的工作流程是这样的:
1) IBAction 被调用以在 2 个不同的表之间切换
2.1) 函数 reloadAllMonth
被调用(用新表的数据初始化一个数组)
2.2) MBProgressHUD *应该显示HUD
3)reloadAllMonth完成
4) HUD应该消失
我当前的代码:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
//sparing you the code that determins where the tap occured
HUD = [[MBProgressHUD alloc]initWithFrame:self.view.frame];
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
[self reloadAllMonth];
allMonthIsActive = YES;
[MBProgressHUD hideHUDForView:self.view animated:YES];
// some other code table reload etc.
}
发生了什么:
-> 函数 touchesbegan 被调用 -> 3-4 秒内没有任何反应(加载时间) -> 弹出新表
问题:为什么 HUD 不显示?我哪里出错了?
最佳答案
你不能在同一个线程中同时显示和隐藏,这就是我要做的:
-(void)reloadStuff
{
[self reloadAllMonth];
allMonthIsActive = YES;
[MBProgressHUD hideHUDForView:self.view animated:YES];
isLoading = NO;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//sparing you the code that determins where the tap occured
if (isLoading)
return;
isLoading = YES;
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
[self performSelectorInBackground:@selector(doStuff) withObject:nil];
}
您也可以删除 ivar HUD,您没有使用它。
并且不要忘记将 viewDidLoad 中的 isLoading 初始化为 NO:
isLoading = NO;
祝你好运!
关于objective-c - MBProgressHUD 不会显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9852288/