我在我的应用程序启动时加载了一个 JSON。
MBProgressHUD 在加载数据时正确显示微调器。
我还有一个刷新按钮,可以触发重新加载 JSON - 我希望它能显示微调器。尽管数据已刷新,但微调器未显示。
知道我做错了什么吗?
这是我的ViewController.m中的相关代码
- (void)viewDidLoad
{
[super viewDidLoad];
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
[self fetchPosts];
}
- (IBAction)refresh:(id)sender {
[MBProgressHUD showHUDAddedTo:self.view animated:YES]; // NOT WORKING
[self refreshPosts];
}
- (void)fetchPosts
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData* data = [NSData dataWithContentsOfURL:[NSURL URLWithString: @"http://mysite.com/app/"]];
NSError* error;
posts = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
dispatch_async(dispatch_get_main_queue(), ^{
[self.collectionView reloadData];
});
});
}
- (void)refreshPosts
{
NSData* data = [NSData dataWithContentsOfURL:[NSURL URLWithString: @"http://mysite.com/app/"]];
NSError* error;
posts = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
dispatch_async(dispatch_get_main_queue(), ^{
[self.collectionView reloadData];
});
}
最佳答案
您是否尝试将 refreshPosts 的整个代码(而不仅仅是对 reloadData 的调用)放入调度 block 中?我当然会尝试看看它是否有效。我认为您的数据下载可能导致 UI 卡住,这可能会搞砸 HUD。
关于objective-c - 重新加载数据时 MBProgressHUD 不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12886042/