我正在使用MBProgressHUD
自定义控件来显示何时从网络加载JSON数据。
我已经在他们的视图中找到了很多没有正确显示HUD控件的答案。
以下是我在视图中显示HUD
的代码。
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
以下是我隐藏HUD
视图的方式。
[MBProgressHUD hideHUDForView:self.view animated:YES];
在ViewDidLoad中,该控件正常运行。
但是当我点击刷新按钮并想要显示HUD控制时,它不显示HUD控制。
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
dispatch_queue_t myqueue = dispatch_queue_create("queue", NULL);
dispatch_async(myqueue, ^{
[self performSelectorOnMainThread:@selector(fetchedData:) withObject:nil waitUntilDone:YES];
dispatch_async(dispatch_get_main_queue(), ^{
[self.tbl reloadData];
[MBProgressHUD hideHUDForView:self.view animated:YES];
});
});
我不知道我做错了什么?请帮帮我。
答案 0 :(得分:9)
将代码切换为:
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
dispatch_queue_t myqueue = dispatch_queue_create("queue", NULL);
dispatch_async(myqueue, ^{
//Whatever is happening in the fetchedData method will now happen in the background
[self fetchedData:nil];
dispatch_async(dispatch_get_main_queue(), ^{
[self.tbl reloadData];
[MBProgressHUD hideHUDForView:self.view animated:YES];
});
});
您不想在主线程上调用fetchData
方法。如果您使用上面的代码,fetchedData
方法中的任何内容都不会在主线程上发生,因此请确保您不更新UI或其中的任何内容。
只是一个建议,我不会为你的"queue"
使用名称dispatch_queue
。应用程序中队列的名称必须是唯一的,因此我将其称为"your.bundle.id.viewcontrollername"
,以避免以后出现问题。