如何经常更新UILabel? [英] How to update a UILabel frequently?

查看:131
本文介绍了如何经常更新UILabel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个项目,我在该项目中请求并解析控制器中的多个html网站。为了向用户提供一些反馈,我创建了第二个视图,该视图在处理数据期间显示。它显示状态标签和进度条。在我的控制器中,我有几点可以更新标签文本。不幸的是,这有时只能起我想那是因为标签只是偶尔会重新绘制一次而我尝试这样做的次数可能超过一次。

I am currently working on a project where I request and parse multiple html sites in a controller. To give some feedback to the user I have created a second view, which gets displayed during processing of the data. It displays a status label and a progressbar. In my controller I have several points where I update my labels text. Unfortunately this works only sometimes. I guess thats because the label gets redrawn only once in a while and I try to do it probably more often than once a second.

它不起作用使用[label setNeedsDisplay]强制重绘;

我还制作了一个主题,用文本更新标签文本全局变量,并且每秒都在标签上调用setNeedsDisplay。但结果却一样。我只看到某些变化,但不是全部。

I also made a thread which updates the labels text with the text of a global variable and also calls setNeedsDisplay on the label every second. But the results in the same. I only see certain changes, but not all.

一切都设置正确,标签永远不会是零。当我记录我的updateMethod时,一切似乎都很好。它只是没有显示!

Everything is setup properly and the label never is nil. When I log my updateMethod everything seems allright. It just does not get displayed!

干杯

这是代码我的主题

- (void)startUpdateStatusThread{

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];  
[self performSelectorOnMainThread:@selector(updateFrequently) withObject:nil waitUntilDone:NO];
[pool release];
}



- (void)updateFrequently{
NSLog(@"updateFrequently: %@", currentStatus);
test++;
[self.statusLabel setText:[NSString stringWithFormat:@"%@ - %i", currentStatus, test]];
[self.statusLabel setNeedsDisplay];
[NSTimer scheduledTimerWithTimeInterval:0.0001 target:self   selector:@selector(updateFrequently) userInfo:nil repeats:NO];
}


推荐答案

你提到线程。请注意,UIKit控件(例如UILabel)只能从主线程更新。确保您只是尝试从主线程设置标签的文本,然后验证您是否仍然遇到问题。

You mention threads. Be aware that UIKit controls, such as UILabel, can only be updated from the main thread. Make sure you are only attempting to set the label's text from the main thread and then verify if you are still having issues.

由于问题编辑而编辑:

首先,-setNeedsDisplay只告诉下次刷新屏幕时需要重新显示的视图,它不会强制显示当时的显示。

First, -setNeedsDisplay only tells the view it needs to redisplay the next time the screen is refreshed, it does not force a display at that time.

其次,iPhone屏幕吸收约60赫兹。这意味着每隔0.016666秒就会有一次更新。您尝试更新的速度要快得多,因此实际上只会显示大约每160个更新一次。

Second, the iPhone screen draws at about 60 hz. This means there is an update every 0.016666 or so seconds. You're trying to update much faster than that, so only about every 160th of your updates will actually be displayed.

第三,也许最重要的是,你没有似乎在制作任何线程,除非你从后台线程调用-startUpdateStatusThread。无论哪种方式,一旦你点击--updateFrequently一切都回到主线程。根据您安排该计时器的频率,您可能会超载CPU并使其成为系统没有时间绘制标签。因此,即使您正在设置新文本,也不会给它渲染的机会。你需要降低那个计时器的频率,你可能需要重新思考你想要做的事情。

Third, and probably most important, you don't seem to be making any threads, unless you call -startUpdateStatusThread from a background thread. Either way, once you hit -updateFrequently everything is back on the main thread. With the frequency you are scheduling that timer, you are probably overloading the CPU and making it so that the system does not have time to draw the label. So even though you are setting new text, you aren't giving it a chance to render. You need to reduce the frequency of that timer, and you probably need to rethink whatever it is you're trying to do.

这篇关于如何经常更新UILabel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆