gpt4 book ai didi

ios - MBProgressHUD 在 iOS 中有时不显示

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:58:23 31 4
gpt4 key购买 nike

我正在使用 MBProgressHUD 自定义控件在从网络加载 JSON 数据时显示。

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

这是我在 View 中显示 HUD 的代码。

[MBProgressHUD showHUDAddedTo:self.view animated:YES];

下面是我如何隐藏我的 HUD View 。

[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];
});
});

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

最佳答案

将您的代码切换为:

[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 或其中的任何内容。

只是一个建议,我不会为您的 dispatch_queue 使用名称 "queue"。应用程序中队列的名称需要是唯一的,所以我将其称为 "your.bundle.id.viewcontrollername" 以避免以后出现问题。

关于ios - MBProgressHUD 在 iOS 中有时不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21886412/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com