ios 调用睡眠(5); 并更新文本字段不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5834062/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calling sleep(5); and updating text field not working
提问by buzzkip
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".
我正在尝试使方法休眠(见下文),而不是textLabel
从 的值更改myTextLabelString
,等待 5 秒,更改为“sleep 5 工作”,等待 5 秒,最后更改为“sleep 5 工作第二次” .... 它只是从 的值开始myTextLabelString
,等待 10 秒,然后更改为“sleep 5 working 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;
}
回答by Thomson Comer
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;
}
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.
有很多方法可以做到这一点。updateTextLabelWithString
您可以使用一个doFirstTextUpdate
写入“sleep 5 工作”的单曲,而不是使用不同的延迟调用两次,然后调用另一个选择器,就像在另一个 5 秒延迟后doSecondTextUpdate
使用相同的[self performSelector:]
技术一样。
It's exceedingly rare that you'll need to use the sleep()
method with Objective-C.
您需要在sleep()
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";
}
回答by Tommy
Changes to UIKit components usually don't take effect until you drop out back to the runloop. Since you never intentionally block the main thread (and, I assume, your code is just a sleep test rather than something you'd actually want to do), that's not normally an issue.
对 UIKit 组件的更改通常在您退出 runloop 之前不会生效。由于您从不故意阻塞主线程(而且,我假设您的代码只是一个睡眠测试,而不是您实际想要做的事情),这通常不是问题。
Try NSLog in place of setting the 'text' property if you really want to verify that sleep is working (all logs have a timestamp), use performSelector:afterDelay: if you want something to occur on the main thread after a pause.
如果您真的想验证睡眠是否正常工作(所有日志都有时间戳),请尝试使用 NSLog 代替设置 'text' 属性,请使用 performSelector:afterDelay: 如果您希望在暂停后在主线程上发生某些事情。
回答by Jonah
As already mentioned, blocking the main thread is probably not what you want to do. Instead of trying to stop your app from doing anything, including redrawing the screen or responding to touches, for 5 seconds think about the problem from a different angle. Create a NSTimer to schedule a method call 5 seconds in the future and let your app keep running while you wait for that timer to fire.
如前所述,阻塞主线程可能不是您想要做的。不要试图阻止你的应用做任何事情,包括重新绘制屏幕或响应触摸,花 5 秒钟从不同的角度思考问题。创建一个 NSTimer 以在未来 5 秒内安排一个方法调用,并让您的应用程序在您等待该计时器触发时继续运行。
回答by Ernest Friedman-Hill
This is a classic issue with virtually all GUI programming toolkits. If you're sleeping on the event-handling thread, then that thread is tied up, and it can't update the screen. If you need to do ongoing work which periodically updates the screen, then you must do that work in a separate thread; that's what you must do here.
这是几乎所有 GUI 编程工具包的经典问题。如果您在事件处理线程上休眠,则该线程被占用,并且无法更新屏幕。如果你需要做定期更新屏幕的持续工作,那么你必须在一个单独的线程中完成这项工作;这就是你在这里必须做的。