我无法更改 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];
稍后,如果我想使用以下代码更改文本的标签,它在应用程序上不会改变:
startLabel.text=@"Searching Database ...";
或
[startLabel setText:@"Searching Database ..."];
UILabel 不为空,我在调试期间将其打印出来并显示:
(gdb) po startLabel
<UILabel: 0x2c1a30; frame = (75 395; 200 30); text = 'Searching Database ...';
clipsToBounds = YES; userInteractionEnabled = NO; layer = <CALayer: 0x2ae8f0>>
因此,标签的文本在 UILabel 内部发生变化,但在屏幕上并未更新。
谁能告诉我什么
我在这里失踪了?谢谢。
编辑1:我尝试了 performSelectorOnMainThread:
- 对我不起作用。
编辑2:我正在使用 AVFoundation 和 ASIHTTP 类来录制声音并在此处上传录制的文件。没有别的了。没有使用任何线程。
I am unable to change the UILabel text. The code for the the UILabel inside viewDidLoad is :
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 :
(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.
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.
发布评论
评论(5)
在我的例子中,正在更新的函数是从线程上的触摸识别器调用的,但是在函数中我更改标签文本属性值的位置我将其放回到主线程上:
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:
我有一个 UILabel 显示的级别号不会更新为 UIViewController 上的新级别号。我能找到的唯一可行的解决方案是在拥有 UILabel 的视图控制器的主线程上调用 setNeedsDisplay
}
请参阅 http://iosdevelopmentjournal.com/blog/2013/01/16/forcing-things-to-run-on-the-main-thread/ 了解更详细的解释
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
}
See http://iosdevelopmentjournal.com/blog/2013/01/16/forcing-things-to-run-on-the-main-thread/ for a more detailed explanation
我的想法有点出乎意料,尽管是合理的——只是没有人想太多。
当我收到在其他地方触发的
NSNotification
时,我应该更新UILabel
。但是,通知是从后台线程触发的,因此即使更新标签文本的侦听器方法也会在后台触发。如果您依赖
NSNotification
来更新标签或任何视图,请从主 UI 线程触发NSNotification
。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 anNSNotification
that I fired somewhere else. However, the notification was fired from a background thread so even the listener methods that update the label's text are fired in the background.If you're relying on an
NSNotification
to update the label, or any of your views, do fire theNSNotification
from the main UI thread.如果您使用的是
localize-Swift
cocoapod,如果您为 UILabel 设置了本地化键
,则该值将在您为标签设置任何文本后设置。In case you are using
localize-Swift
cocoapod, If you set alocalized key
to the UILabel, The value will be set after you set any text to the label.您可能会遇到上面评论中提到的线程问题。如果您有一个在主线程上运行并执行某些活动(例如搜索数据库)的方法,则在运行循环获得控制权之前,您对 UI 所做的更新将不会提交。因此,如果您在主线程上执行一项耗时的任务,请在设置标签文本后运行此代码:
这应该会清除您所做的任何 UI 更改。尽管这可能是一个明智且实用的技巧,但它并没有打败其他选择。我强烈建议您在后台线程上执行应用的耗时任务,并使用
performSelectorOnMainThread:withObject:waitUntilDone:
通过主线程更新 UI。请参阅 Apple 的 iOS 线程管理 页面。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:
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.