Cocoa Touch - 加载 UITabBar 视图时显示活动指示器

发布于 2024-10-09 05:06:30 字数 390 浏览 0 评论 0原文

我有一个 UITabBar 应用程序,有两个视图,它们在“viewWillAppear”方法中从网络加载大量数据。我想在检索此数据时显示进度条或活动指示器,以确保用户知道应用程序没有冻结。

我知道以前已经有人问过这个问题。我只需要澄清一下似乎是一个相当好的解决方案

我已经实现了示例中的代码。该问题的最初提问者后来通过将数据检索放入另一个“线程”中解决了他们的问题。我理解线程的概念,但我不知道如何实现这一点。

通过研究,我发现我需要将所有繁重的数据检索移至后台线程,因为所有 UI 更新都发生在主线程中。

如果有人愿意为我提供一个例子,我将非常感激。我可以根据需要提供部分现有代码。

I have a UITabBar Application with two views that load large amounts of data from the web in their "viewWillAppear" methods. I want to show a progress bar or an activity indicator while this data is being retrieved, to make sure the user knows the app isn't frozen.

I am aware that this has been asked before. I simply need some clarification on what seems to be a rather good solution.

I have implimented the code in the example. The question's original asker later solved their problem, by putting the retrieval of data into another "thread". I understand the concept of threads, but I do not know how I would impliment this.

With research, I have found that I need to move all of my heavy data retrieval into a background thread, as all of the UI updating occurs in the main thread.

If one would be so kind as to provide an example for me, I would be very appreciative. I can provide parts of my existing code as necessary.

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

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

发布评论

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

评论(1

魂归处 2024-10-16 05:06:30

如果您使用 NSURLConnection 它会自动在另一个线程上运行。

在你的 viewDidLoad: 中

NSURLRequest *req = [NSURLRequest requestWithURL:theURL];
NSURLConnection *conn = [NSURLConnection connectionWithRequest:req delegate:self];

,那么你需要一些自定义方法。如果您输入 -connection 并按 Esc 键,您将看到可以使用的所有不同方法。为此,您需要三个:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    // this is called when there is a response
        // if you're collecting data init your NSMutableData here
}

-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    // each time the connection downloads a 
        // packet of data it gets send here
        // so you can do [myData appendData:data];
} 

- (void) connectionDidFinishLoading:(NSURLConnection *)connection {
        // the connection has finished so you can 
        // do what you want with the data here
}

这基本上就是全部了。 NSURLConnection 本身处理所有多线程,您不必担心。现在您可以创建一个活动指示器并显示它,它会起作用,因为主线程是空的。 :)

If you use NSURLConnection it runs on another thread automatically.

in your viewDidLoad:

NSURLRequest *req = [NSURLRequest requestWithURL:theURL];
NSURLConnection *conn = [NSURLConnection connectionWithRequest:req delegate:self];

then you need some custom methods. If you type in -connection and press Esc you'll see all the different methods you can use. There are three you will need with this:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    // this is called when there is a response
        // if you're collecting data init your NSMutableData here
}

-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    // each time the connection downloads a 
        // packet of data it gets send here
        // so you can do [myData appendData:data];
} 

- (void) connectionDidFinishLoading:(NSURLConnection *)connection {
        // the connection has finished so you can 
        // do what you want with the data here
}

That is basically all there is to it. NSURLConnection handles all the multithreading itself and you don't have to worry. Now you can create an activity indicator and display it and it will work because the main thread is empty. :)

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