在 NSURLConnection 下载期间更新 iPhone UIProgressView
我正在使用此代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
self.oDPVC
的值是多少(是nil
)吗?self.oDPVC.oProgress
的值是多少(是nil
)?其他几点。
第一的:
您的
oDPVC
@property
是如何定义的?如果它使用retain
,此行稍后可能会导致内存泄漏(它将被双重retain
-ed)。您应该改用此模式:第二:
progress
属性本身就是一个浮点数。您实际上并不需要使用NSNumber
对象。您可能还需要考虑在分母上加一以避免被零除:What is the value of
self.oDPVC
(is itnil
)?What is the value of
self.oDPVC.oProgress
(is itnil
)?A couple of other points.
First:
How is your
oDPVC
@property
defined? If it usesretain
, this line could (will) later result a memory leak (it will be doubleretain
-ed). You should use this pattern instead:Second:
The
progress
property is a float itself. You don't really need to use anNSNumber
object. You also might want to consider adding one to the denominator to avoid divide-by-zero: