在 NSURLConnection 下载期间更新 iPhone UIProgressView

发布于 2024-08-26 06:17:36 字数 1119 浏览 11 评论 0原文

我正在使用此代码:

NSURLConnection *oConnection=[[NSURLConnection alloc] initWithRequest:oRequest delegate:self];

下载文件,并且我想更新我加载的子视图上的进度条。为此,我使用以下代码:

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [oReceivedData appendData:data];
 float n = oReceivedData.length;
 float d = self.iTotalSize;
 NSNumber *oNum = [NSNumber numberWithFloat:n/d];
 self.oDPVC.oProgress.progress = [oNum floatValue];
}

子视图是 oDPVC,其进度条是 oProgress。设置进度属性不会更新控件。

根据我在网上看到的信息,很多人都想这样做,但没有一个完整、可靠的示例。此外,还有很多相互矛盾的建议。有些人认为不需要单独的线程。有人说你需要一个后台线程来更新进度。有人说不行,在后台做其他事情,在主线程上更新进度。

我已经尝试了所有的建议,但没有一个对我有用。

还有一件事,也许这是一条线索。我使用此代码在 applicationDidFinishLaunching 期间加载子视图:

self.oDPVC = [[DownloadProgressViewController alloc] initWithNibName:@"DownloadProgressViewController" bundle:nil];
[window addSubview:self.oDPVC.view];

在 XIB 文件(我已在 Interface Builder 和文本编辑器中检查过)中,进度条为 280 像素宽。但是当视图打开时,它已以某种方式调整为宽度的一半。另外,视图的背景图像是default.png。它不是出现在默认图像的正上方,而是被推高了大约 10 个像素,在屏幕底部留下了一个白条。

也许这是一个单独的问题,也许不是。

I am using this code:

NSURLConnection *oConnection=[[NSURLConnection alloc] initWithRequest:oRequest delegate:self];

to download a file, and I want to update a progress bar on a subview that I load. For that, I use this code:

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [oReceivedData appendData:data];
 float n = oReceivedData.length;
 float d = self.iTotalSize;
 NSNumber *oNum = [NSNumber numberWithFloat:n/d];
 self.oDPVC.oProgress.progress = [oNum floatValue];
}

The subview is oDPVC, and the progress bar on it is oProgress. Setting the progress property does not update the control.

From what I have read on the Web, lots of people want to do this and yet there is not a complete, reliable sample. Also, there is much contradictory advice. Some people think that you don't need a separate thread. Some say you need a background thread for the progress update. Some say no, do other things in the background and update the progress on the main thread.

I've tried all the advice, and none of it works for me.

One more thing, maybe this is a clue. I use this code to load the subview during applicationDidFinishLaunching:

self.oDPVC = [[DownloadProgressViewController alloc] initWithNibName:@"DownloadProgressViewController" bundle:nil];
[window addSubview:self.oDPVC.view];

In the XIB file (which I have examined in both Interface Builder and in a text editor) the progress bar is 280 pixels wide. But when the view opens, it has somehow been adjusted to maybe half that width. Also, the background image of the view is default.png. Instead of appearing right on top of the default image, it is shoved up about 10 pixels, leaving a white bar across the bottom of the screen.

Maybe that's a separate issue, maybe not.

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

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

发布评论

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

评论(1

哆啦不做梦 2024-09-02 06:17:36
self.oDPVC.oProgress.progress = [oNum floatValue];

设置进度属性不会
更新控件。

self.oDPVC 的值是多少(是 nil)吗?

self.oDPVC.oProgress 的值是多少(是 nil)?

其他几点。

第一的:

self.oDPVC = [[DownloadProgressViewController alloc] initWithNibName:@"DownloadProgressViewController"bundle:nil];

您的 oDPVC @property 是如何定义的?如果它使用retain,此行稍后可能会导致内存泄漏(它将被双重retain-ed)。您应该改用此模式:

DownloadProgressViewController* dpvc = [[DownloadProgressViewController alloc] initWithNibName:@"DownloadProgressViewController" bundle:nil];
self.oDPVC = dpvc;
[dpvc release];

第二:

float n = oReceivedData.length;
float d = self.iTotalSize;
NSNumber *oNum = [NSNumber numberWithFloat:n/d];
self.oDPVC.oProgress.progress = [oNum floatValue];

progress 属性本身就是一个浮点数。您实际上并不需要使用 NSNumber 对象。您可能还需要考虑在分母上加一以避免被零除:

float n = oReceivedData.length;
float d = self.iTotalSize;
float percentCompleted = n/(d + 1.0f);
self.oDPVC.oProgress.progress = percentCompleted;
self.oDPVC.oProgress.progress = [oNum floatValue];

Setting the progress property does not
update the control.

What is the value of self.oDPVC (is it nil)?

What is the value of self.oDPVC.oProgress (is it nil)?

A couple of other points.

First:

self.oDPVC = [[DownloadProgressViewController alloc] initWithNibName:@"DownloadProgressViewController"bundle:nil];

How is your oDPVC @property defined? If it uses retain, this line could (will) later result a memory leak (it will be double retain-ed). You should use this pattern instead:

DownloadProgressViewController* dpvc = [[DownloadProgressViewController alloc] initWithNibName:@"DownloadProgressViewController" bundle:nil];
self.oDPVC = dpvc;
[dpvc release];

Second:

float n = oReceivedData.length;
float d = self.iTotalSize;
NSNumber *oNum = [NSNumber numberWithFloat:n/d];
self.oDPVC.oProgress.progress = [oNum floatValue];

The progress property is a float itself. You don't really need to use an NSNumber object. You also might want to consider adding one to the denominator to avoid divide-by-zero:

float n = oReceivedData.length;
float d = self.iTotalSize;
float percentCompleted = n/(d + 1.0f);
self.oDPVC.oProgress.progress = percentCompleted;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文