iPhone UILabel 未更新
我知道周围有很多类似的问题,但似乎没有一个答案可以解决我的问题。我有一个应用程序,它使用 NSURLConnection 下载文件,然后对下载的文件进行一些计算。我设置了一个 UILabel 来显示当前加载状态(例如:“正在加载文件”、“正在解析文件”)。我更新了 NSURLConnection 委托的 didReceiveResponse 和 connectionDidFinishLoading 函数中的 UILabel,以及代码中的其他一些位置。我通过调用以下函数来更新它:
[self performSelectorOnMainThread:@selector(updateProgress) withObject:nil waitUntilDone:NO]
其中 -(void)updateProgress 是我定义的调用 [theLabel setNeedsDisplay] 的函数。我对它进行了 NSLog 处理,
NSLog(@"theLabel: %@\n",theLabel.text);
信息已正确更新,但标签实际上并未在视图中更新。此外, updateProgress 仅在所有内容加载后才会调用。它更新了标签 THEN,这几乎没有用。有什么建议吗?
I know there are lots of similar questions floating around, but none of the answers seem to fix my problem. I have an app that uses an NSURLConnection to download a file, and then does some calculations on the downloaded file. I set up a UILabel to display the current loading status (eg: "Loading file", "Parsing file"). I update the UILabel in the didReceiveResponse and connectionDidFinishLoading function of the NSURLConnection delegate, as well as some other places in my code. I update it by calling the following function:
[self performSelectorOnMainThread:@selector(updateProgress) withObject:nil waitUntilDone:NO]
where -(void)updateProgress is a function I defined to call [theLabel setNeedsDisplay]. I NSLog'd it, like
NSLog(@"theLabel: %@\n",theLabel.text);
and the information is updated correctly, but the label doesn't actually update in the view. Also, updateProgress is only called AFTER everything is loaded. It updates the label THEN, which is hardly useful. Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
NSURLConnection 正在阻塞主线程(在完成之前不会对视图执行任何更新)。
您可以在后台执行
updateProgress
:updateProgress
的第一行应该是:最后几行应该是:
http://developer.apple.com/library/ios/#documentation/Cocoa/Reference /Foundation/Classes/NSAutoreleasePool_Class/Reference/Reference.html
当然,你也可以在后台执行NSURLConnection。然后你可以在主线程上更新标签。
The NSURLConnection is blocking the main thread (no updates will be performed on the view until it finishes).
You can perform
updateProgress
in the background:The first line of
updateProgress
should be:The last lines should be:
http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSAutoreleasePool_Class/Reference/Reference.html
Of course, you can also perform the NSURLConnection in the background. Then you can update the label on the main thread.