I'm very very new to iOS programming but I already have a big issue I can't solve. It seems so easy.
我对iOS编程非常陌生,但我已经遇到了一个无法解决的大问题。看起来很容易。
I have a button, I click on it to change a label called message
我有一个按钮,我点击它来更改一个名为message的标签
Here is the code:
这是代码:
- (IBAction)react:(id)sender {
int hasard ;
hasard=3;
message.text=[NSString stringWithFormat:@"1 %d",hasard];
sleep (1);
message.text=[NSString stringWithFormat:@"2 %d",hasard];
}
It works well but I don't see the first message.text
change. When I click the button, I have to wait one second and I see 2 3
它运作良好,但我没有看到第一个message.text更改。当我点击按钮时,我必须等待一秒钟,然后我会看到2 3
I thought I could see 1 3
, wait a second and then see 2 3
. What is missing? It seems so obvious.
我以为我能看到1 3,等一下然后看到2 3.缺少什么?这似乎很明显。
2 个解决方案
#1
1
You are blocking the main thread (well, rather sleep()
blocks it). If you do so, committed changes to the UI won't appear - you'll only see the final result. You have to do something else (blocking the UI is a very bad idea in terms of user experience, by the way). You can try using a timer, for example:
你正在阻塞主线程(好吧,而不是sleep()阻塞它)。如果这样做,则不会显示对UI的已提交更改 - 您只会看到最终结果。您必须做其他事情(顺便说一下,阻止UI在用户体验方面是一个非常糟糕的主意)。您可以尝试使用计时器,例如:
int hasard = 3;
- (void)react:(id)sender
{
message.text = [NSString stringWithFormat:@"1 %d", hasard];
[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(timer:) userInfo:nil repeats:NO];
}
- (void)timer:(NSTimer *)tmr
{
message.text = [NSString stringWithFormat:@"2 %d", hasard];
}
#2
6
sleep()
will suspend the current threads execution which is the main thread so you are blocking all UI operations until the method completes. You need to schedule the second assignment to run after the specified time without blocking the current run loop. This can be achieved with GCD.
sleep()将挂起当前线程执行,这是主线程,因此您将阻止所有UI操作,直到方法完成。您需要安排第二个分配在指定时间后运行,而不会阻止当前运行循环。这可以通过GCD实现。
- (IBAction)react:(id)sender {
int hasard ;
hasard=3;
message.text=[NSString stringWithFormat:@"1 %d",hasard];
int64_t delayInSeconds = 1.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
message.text=[NSString stringWithFormat:@"2 %d",hasard];
});
}
#1
1
You are blocking the main thread (well, rather sleep()
blocks it). If you do so, committed changes to the UI won't appear - you'll only see the final result. You have to do something else (blocking the UI is a very bad idea in terms of user experience, by the way). You can try using a timer, for example:
你正在阻塞主线程(好吧,而不是sleep()阻塞它)。如果这样做,则不会显示对UI的已提交更改 - 您只会看到最终结果。您必须做其他事情(顺便说一下,阻止UI在用户体验方面是一个非常糟糕的主意)。您可以尝试使用计时器,例如:
int hasard = 3;
- (void)react:(id)sender
{
message.text = [NSString stringWithFormat:@"1 %d", hasard];
[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(timer:) userInfo:nil repeats:NO];
}
- (void)timer:(NSTimer *)tmr
{
message.text = [NSString stringWithFormat:@"2 %d", hasard];
}
#2
6
sleep()
will suspend the current threads execution which is the main thread so you are blocking all UI operations until the method completes. You need to schedule the second assignment to run after the specified time without blocking the current run loop. This can be achieved with GCD.
sleep()将挂起当前线程执行,这是主线程,因此您将阻止所有UI操作,直到方法完成。您需要安排第二个分配在指定时间后运行,而不会阻止当前运行循环。这可以通过GCD实现。
- (IBAction)react:(id)sender {
int hasard ;
hasard=3;
message.text=[NSString stringWithFormat:@"1 %d",hasard];
int64_t delayInSeconds = 1.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
message.text=[NSString stringWithFormat:@"2 %d",hasard];
});
}