问题描述
我尝试睡眠一个方法(见下文),而不是 textLabel
从的值更改myTextLabelString
,等待5秒,更改为睡眠5工作,等待5秒,最后更改为睡眠5工作第二次回合....它只是从的值myTextLabelString
,等待10秒,然后更改为sleep 5 working second time round。
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;
}
推荐答案
您寻求:
-(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;
}
有很多方法可以做到这一点。而不是有一个 updateTextLabelWithString
你调用两次不同的延迟,你可以有一个 doFirstTextUpdate
写入sleep 5工作,然后再调用另一个选择器如 doSecondTextUpdate
使用相同的 [self performSelector:]
。
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()
/ p>
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";
}
这篇关于调用sleep(5);并更新文本字段不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!