iphone - 如何频繁更新UILabel?

标签 iphone uilabel nsthread

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

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

我还创建了一个线程,它使用全局变量的文本更新标签文本,并且每秒在标签上调用 setNeedsDisplay 。但结果是一样的。我只看到某些变化,但不是全部。

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

干杯

这是我的线程的代码

- (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];
}

最佳答案

如果我假设您从解析您网站的“线程”中调用标签的文本属性以及 setNeedsDisplay ,我对吗?

请注意:对 UIView(或 UIView 的子类,如标签)的更改必须在主线程上执行。

我建议您编写一个辅助方法来更新您的标签并调用 setNeedDisplay(来自 Parser-Thread 调用)

[mainViewController performSelectorOnMainThread:@selector(yourUpdateLabelHelper:) withObject:nil waitUntilDone:NO];

这应该可以完成你的工作。

关于iphone - 如何频繁更新UILabel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3458888/

相关文章:

ios - iOS 9 升级后无法从 diawi 安装 iPhone 应用程序?

ios - 设备自动锁定时 AVPlayer 在 iPad 上不工作

ios - 在 Swift 中从文本创建超链接

iphone - 为什么没有performSelectorOnMainThread : in NSObject Protocol?

iphone - 每个线程都需要自己的自动释放池吗?

iphone - 可编辑的表格 View 单元格

iphone - 使用 Pinterest 登录

ios - 如何实现清晰渲染的旋转 UILabel?

ios - 调整字体大小以适合边界矩形?

iphone - 下载时停止 NSThread