调用睡眠(5);和更新文本字段不起作用 [英] Calling sleep(5); and updating text field not working
问题描述
我正在尝试使方法休眠(见下文),而不是 textLabel
从 myTextLabelString
的值更改,等待 5 秒,更改为sleep5 工作",等待 5 秒,最后更改为睡眠 5 工作第二次"......它只是从 myTextLabelString
的值开始,等待 10 秒,然后更改为睡眠 5"第二次工作".
I'm trying to sleep a method (see below), but rather than the textLabel
changing from the value of myTextLabelString
, waiting 5 seconds, changing to "sleep 5 worked", waiting 5 seconds and finally changing to "sleep 5 worked second time round".... it just goes from the value of myTextLabelString
, waits 10 seconds, and then changes to "sleep 5 worked second time round".
- (void)textLabelChanger:(id)sender {
NSString *myTextLabelString = [NSString stringWithFormat:@"%d", gameCountDown];
textLabel.text=myTextLabelString;
sleep(5);
textLabel.text=@"sleep 5 worked";
sleep(5);
textLabel.text=@"sleep 5 worked second time round";
return;
}
推荐答案
这可能会提供您寻求的结果:
This will probably provide the result that you seek:
-(void)textLabelChanger:(id)sender
{
NSString *myTextLabelString = [NSString stringWithFormat:@"%d", gameCountDown];
textLabel.text=myTextLabelString;
[self performSelector:@selector(updateTextLabelWithString:) withObject:@"sleep 5 worked" afterDelay:5.0];
[self performSelector:@selector(updateTextLabelWithString:) withObject:@"sleep 5 worked second time round" afterDelay:10.0];
}
-(void)updateTextLabelWithString:(NSString*)theString
{
textLabel.text=theString;
}
有很多方法可以做到这一点.您可以使用 doFirstTextUpdate
编写sleep 5 working",然后调用另一个选择器,例如 ,而不是使用一个
使用相同的 updateTextLabelWithString
以不同的延迟调用两次doSecondTextUpdate[self performSelector:]
技术,再延迟 5 秒.
There are plenty of ways to do this. Instead of having a single updateTextLabelWithString
that you call twice with different delays, you could have a doFirstTextUpdate
that writes the "sleep 5 worked" and then calls another selector like doSecondTextUpdate
using the same [self performSelector:]
technique after another 5 second delay.
在 Objective-C 中使用 sleep()
方法的情况极为罕见.
It's exceedingly rare that you'll need to use the sleep()
method with Objective-C.
-(void)textLabelChanger:(id)sender
{
NSString *myTextLabelString = [NSString stringWithFormat:@"%d", gameCountDown];
textLabel.text=myTextLabelString;
[self performSelector:@selector(firstUpdate) withObject:nil afterDelay:5.0];
}
-(void)firstUpdate
{
textLabel.text = @"sleep 5 worked";
[self performSelector:@selector(secondUpdate) withObject:nil afterDelay:5.0];
}
-(void)secondUpdate
{
textLabel.text = @"sleep 5 worked second time round";
}
这篇关于调用睡眠(5);和更新文本字段不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!