ios UILabel 文本未更新
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6835472/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UILabel text not being updated
提问by Ahsan
I am unable to change the UILabel text. The code for the the UILabel inside viewDidLoad is :
我无法更改 UILabel 文本。viewDidLoad 中 UILabel 的代码是:
startLabel=[[UILabel alloc] initWithFrame:CGRectMake(75, 395, 200, 30)];
startLabel.text=@"Recording Sound ...";
startLabel.backgroundColor=[UIColor clearColor];
startLabel.textColor=[UIColor whiteColor];
startLabel.font=[UIFont boldSystemFontOfSize:17];
[self.view addSubview:startLabel];
Later, if I want to change the label of the text with the following code, its not changing on the app :
稍后,如果我想使用以下代码更改文本的标签,则不会在应用程序上更改:
startLabel.text=@"Searching Database ...";
or
或者
[startLabel setText:@"Searching Database ..."];
The UILabel is not empty, I printed it out during debugging and it shows :
UILabel 不为空,我在调试期间将其打印出来并显示:
(gdb) po startLabel
<UILabel: 0x2c1a30; frame = (75 395; 200 30); text = 'Searching Database ...';
clipsToBounds = YES; userInteractionEnabled = NO; layer = <CALayer: 0x2ae8f0>>
So, the text of the label changes inside the UILabel, but its not updated on the screen.
因此,标签的文本在 UILabel 内会发生变化,但不会在屏幕上更新。
Can anyone kindly let me know what I am missing here ? Thanks.
任何人都可以让我知道我在这里缺少什么吗?谢谢。
Edit 1: I tried performSelectorOnMainThread:
- didnt work for me.
Edit 2: I am using AVFoundation
and ASIHTTP
classes to record sound and upload the recorded file here. Nothing else. Didnt use any thread.
编辑 1:我试过performSelectorOnMainThread:
- 对我不起作用。编辑 2:我正在使用AVFoundation
和ASIHTTP
类来录制声音并在此处上传录制的文件。没有其他的。没有使用任何线程。
回答by ndoc
In my case the function that was updating was called from a touch recognizer on a thread, but the place in the function where I'm changing the value of the label's text property I put it back on the main thread:
在我的情况下,正在更新的函数是从线程上的触摸识别器调用的,但是在函数中我更改标签文本属性值的位置我将它放回主线程:
dispatch_async(dispatch_get_main_queue(), ^{
[self.someLabel setText:someString];
});
回答by Alex Nichol
You may be facing an issue with threading as mentioned in the comments above. If you have a method that runs on the main thread and does some activity (such as search a database), updates that you make to the UI will not be committed until the run loop gets control. So, if you have a long, time consuming task going on on the main thread, run this code after setting the text of the label:
您可能会遇到上面评论中提到的线程问题。如果您有一个在主线程上运行并执行某些活动(例如搜索数据库)的方法,则在运行循环获得控制之前不会提交您对 UI 所做的更新。因此,如果您在主线程上执行长时间、耗时的任务,请在设置标签文本后运行以下代码:
- (void)doSomethingTimeConsuming
... consume some time ...
... set text of label ...
[[NSRunLoop mainRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.5]];
... continue long operation ...
}
This should flush out any UI changes that you have made. Although this may be a sensible and functional hack, it doesn't beat the alternative. I highly suggest that you perform your app's time consuming tasks on a background thread, and update the UI through the main thread using performSelectorOnMainThread:withObject:waitUntilDone:
. See Apple's page on iOS Thread Management.
这应该清除您所做的任何 UI 更改。尽管这可能是一种明智且实用的 hack,但它并没有击败替代方案。我强烈建议您在后台线程上执行应用程序的耗时任务,并使用performSelectorOnMainThread:withObject:waitUntilDone:
. 请参阅 Apple 关于iOS 线程管理的页面。
回答by Matthew Quiros
Mine's a bit more unexpected, though reasonable--just not something anyone thinks too hard about.
我的有点出乎意料,虽然合理——只是没有人想得太过。
I'm supposed to update the UILabel
when I receive an NSNotification
that I fired somewhere else. However, the notification was fired from a background threadso even the listener methods that update the label's text are fired in the background.
UILabel
当我收到NSNotification
我在其他地方解雇的消息时,我应该更新。但是,通知是从后台线程触发的,因此即使是更新标签文本的侦听器方法也会在后台触发。
If you're relying on an NSNotification
to update the label, or any of your views, do fire the NSNotification
from the main UI thread.
如果您依赖 anNSNotification
来更新标签或任何视图,请务必NSNotification
从主 UI 线程中触发。
回答by user1233894
I had a UILabel showing a level number that would not update to the new level number on a UIViewController. The only viable solution I could find that worked was to call setNeedsDisplay on the main thread of the view controller that owned the UILabel
我有一个 UILabel 显示一个级别编号,该编号不会更新为 UIViewController 上的新级别编号。我能找到的唯一可行的解决方案是在拥有 UILabel 的视图控制器的主线程上调用 setNeedsDisplay
-(void)changeLevelLabel:(int)theLevel {
dispatch_async(dispatch_get_main_queue(), ^{
self.levelLabel.text = [NSString stringWithFormat:@"%d",theLevel];
[self.levelLabel setNeedsDisplay];
});
}
}
See http://iosdevelopmentjournal.com/blog/2013/01/16/forcing-things-to-run-on-the-main-thread/for a more detailed explanation
有关更详细的说明,请参阅http://iosdevelopmentjournal.com/blog/2013/01/16/forcing-things-to-run-on-the-main-thread/