我想从单独的线程更新 iPhone 表单上的标签。线程正在运行,但标签未更新。
这是我的代码
-(void)launchThread:(id)param;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
...
-(void) launchThread:(id)param{
UILabel *control = (UILabel*)param;
NSMutableString *str;
for (int i=0; i<1000; i++) {
str = [NSMutableString stringWithFormat:@"%d",i];
control.text=str;
NSLog(@"%@",str);
sleep(1);
}}
...
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[NSThread detachNewThreadSelector:@selector(launchThread:) toTarget:self withObject:timeLabel];
}
我猜标签的文本已更新,但表单上未刷新。如何刷新?
我尝试寻找类似的帖子,但它们对我不起作用。抱歉,如果我重复了这个问题。
最佳答案
@jackslash是正确的,只能在主线程(UI线程)上更新界面。我建议使用 performSelectorOnMainThread
来更新您的 UILabel。我认为语法是这样的:
[self.label performSelectorOnMainThread : @ selector(setText : ) withObject:str waitUntilDone:YES];
不过还不太确定。进行一些谷歌搜索,您应该能够找到一个可用的示例。
关于iPhone 标签未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9088766/