UILabel 文本未更新 [英] UILabel text not being updated

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

问题描述

我无法更改 UILabel 文本.viewDidLoad 中 UILabel 的代码是:

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 ...";

[startLabel setText:@"Searching Database ..."];

UILabel 不为空,我在调试时打印出来,显示:

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>>

因此,标签的文本在 UILabel 内发生了变化,但在屏幕上并未更新.

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.

编辑 1:我尝试了 performSelectorOnMainThread: - 对我不起作用.编辑 2:我使用 AVFoundationASIHTTP 类来录制声音并在此处上传录制的文件.没有其他的.没有使用任何线程.

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.

推荐答案

您可能正面临上述评论中提到的线程问题.如果您有一个在主线程上运行并执行某些活动(例如搜索数据库)的方法,则在运行循环获得控制之前不会提交您对 UI 所做的更新.因此,如果您在主线程上执行长时间、耗时的任务,请在设置标签文本后运行以下代码:

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:

- (void)doSomethingTimeConsuming
    ... consume some time ...
    ... set text of label ...
    [[NSRunLoop mainRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.5]];
    ... continue long operation ...
}

这应该会清除您所做的任何 UI 更改.尽管这可能是一个明智且实用的黑客攻击,但它并没有击败替代方案.我强烈建议您在后台线程上执行应用程序的耗时任务,并使用 performSelectorOnMainThread:withObject:waitUntilDone: 通过主线程更新 UI.请参阅 Apple 在 iOS 线程管理上的页面.

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.

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

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