MBProgressHUD在iOS中没有显示

Fir*_*ist 2 json ios mbprogresshud

我正在使用MBProgressHUD自定义控件来显示从Web加载JSON数据.

我已经在他们的视图中找到了很多没有正确显示HUD控件的答案.

这是HUD我的视图中显示的代码.

[MBProgressHUD showHUDAddedTo:self.view animated:YES];
Run Code Online (Sandbox Code Playgroud)

这就是我隐藏HUD视图的方式.

[MBProgressHUD hideHUDForView:self.view animated:YES];
Run Code Online (Sandbox Code Playgroud)

在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];
    });
});
Run Code Online (Sandbox Code Playgroud)

我不知道我做错了什么?请帮我.

cod*_*der 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];
    });
});
Run Code Online (Sandbox Code Playgroud)

您不想fetchData在主线程上调用该方法.如果您使用上面的代码,fetchedData方法中的任何内容都不会在主线程上发生,因此请确保您不更新UI或其中的任何内容.

只是一个建议,我不会用"queue"你的名字dispatch_queue.应用程序中队列的名称必须是唯一的,所以我称之为"your.bundle.id.viewcontrollername"仅仅是为了避免以后出现问题.