iPhone UILabel 未更新

发布于 2024-10-01 04:57:50 字数 613 浏览 0 评论 0原文

我知道周围有很多类似的问题,但似乎没有一个答案可以解决我的问题。我有一个应用程序,它使用 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 技术交流群。

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

发布评论

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

评论(1

み格子的夏天 2024-10-08 04:57:50

NSURLConnection 正在阻塞主线程(在完成之前不会对视图执行任何更新)。

您可以在后台执行 updateProgress

[self performSelectorInBackground:@selector(updateProgress) withObject:nil]

updateProgress 的第一行应该是:

NSAutoReleasePool *pool = [[NSAutoReleasePool alloc]init];

最后几行应该是:

[pool release];
pool = nil;

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:

[self performSelectorInBackground:@selector(updateProgress) withObject:nil]

The first line of updateProgress should be:

NSAutoReleasePool *pool = [[NSAutoReleasePool alloc]init];

The last lines should be:

[pool release];
pool = nil;

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.

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