数据加载时显示活动指示器时出现问题

发布于 2024-11-04 18:40:58 字数 636 浏览 4 评论 0原文

我觉得好像有一个非常简单的解决方案来解决我的问题,但到目前为止我几乎没有成功...我想加载我的初始 .xib 文件(提前退出 default.png 启动屏幕),以便我可以显示加载我的 html 数据并设置我的 xib 文件创建的文本标签字段时的活动指示器。

不幸的是,当我执行下面的代码时,我会显示 default.png 图像,直到从每个网站加载所有数据为止...我可以更改什么才能首先显示我的 mainView,然后启动活动指示器,加载我的html 数据,并在我的 mainView 中设置文本标签?

@implementation MainViewController
- (void)viewDidLoad {
[super viewDidLoad];

[activityIndicator startAnimating];

[self runTimer];

}

- (void)viewDidAppearBOOL)animated {

[super viewDidAppear:animated];

[self loadHTMLData1];

[self loadHTMLData2];

[self loadHTMLData3];

[self loadHTMLData4];

[activityIndicator stopAnimating];

}
...

I feel as though there is a really simple solution to my problem, but thus far I have had little success... I want to load my initial .xib file (exiting the default.png splash screen early), so that I may display an activity indicator while loading my html data and setting the text label fields created by my xib file.

Unfortunately, when I execute the following code below, I display my default.png image until all of the data is loaded from each website... What may I change in order to first display my mainView, then start the activity indicator, load my html data, and set the text labels in my mainView?

@implementation MainViewController
- (void)viewDidLoad {
[super viewDidLoad];

[activityIndicator startAnimating];

[self runTimer];

}

- (void)viewDidAppearBOOL)animated {

[super viewDidAppear:animated];

[self loadHTMLData1];

[self loadHTMLData2];

[self loadHTMLData3];

[self loadHTMLData4];

[activityIndicator stopAnimating];

}
...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

〆一缕阳光ご 2024-11-11 18:40:58

这都与 iOS 如何更新 ui 有关。当你调用

[activityIndicator startAnimating];

它并不意味着立即开始动画,它意味着你告诉用户界面下次更新显示时,开始动画。

所有这些更新都发生在主线程上(如果您还没有创建线程,那么您已经在主线程上),因此如果您执行其他需要很长时间的操作,它会在更新之前执行此操作显示

有几种方法可以解决这个问题,它们都涉及创建另一个在后台运行的线程。

看看 NSOperation (和 NSOperationQueue) - 这将让您对 iOS 将在后台运行的各个任务进行排队。然后,当它们完成后,您可以再次更新显示并关闭活动指示器。

NSOperationQueue 教程遍布google :)

希望有帮助。

It's all to do with how iOS updates the ui. When you call

[activityIndicator startAnimating];

it doesn't mean start animating immediately, it means you're telling the ui the next time you are updating the display, start animating.

All of this updating happens on the main thread (if you haven't made a thread, you're already on the main thread) so if you do something else that takes a long time, it will do this before updating the display.

There are a few ways to fix this and they all involve making another thread that runs in the background.

Take a look at NSOperation (and NSOperationQueue) - this will let you queue up individual tasks that iOS will run in the background for you. then when they are complete you can update your display again and turn off your activity indicator.

There's NSOperationQueue tutorials all over google :)

Hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文