objective-c - 重新加载数据时 MBProgressHUD 不显示

标签 objective-c ios ios5 mbprogresshud

我在我的应用程序启动时加载了一个 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/

相关文章:

objective-c - 为什么要初始化父类

ios - UITableView 阴影不正确(未对齐)

ios - Objective-C 中的 Grand Central Dispatch 保证了哪些行为?

ios - Objective-C - 设置 ImageView 宽度并保持纵横比

objective-c - Cocoa 中的多行 PDFAnnotationTextWidget

ios - 如何在不打开 xcode 的情况下将 ionic 构建推送到 ios 设备?

ios - 如何在单行中的两个单元格之间设置没有间隙的 Collection View 单元格

objective-c - JTRevealSideBar splitview添加UISwipeGesture

ios5 - UIButton 内部事件在 ios 5 中不起作用,而在 ios 6 中则可以

audio - 自动语音匹配的开源?