ios - iOS:如何刷新功能中的 View ?

标签 ios

我对iOS编程非常陌生,但是我已经遇到了一个无法解决的大问题。似乎很容易。

我有一个按钮,单击它以更改名为message的标签

这是代码:

- (IBAction)react:(id)sender {
    int hasard ;
    hasard=3; 
    message.text=[NSString stringWithFormat:@"1 %d",hasard];    
    sleep (1);
    message.text=[NSString stringWithFormat:@"2 %d",hasard]; 
}

它工作正常,但我看不到第一个message.text更改。
当我单击按钮时,我必须等待一秒钟,然后看到2 3
我以为我可以看到1 3,请稍等片刻,然后再看到2 3
什么东西少了?似乎很明显。

最佳答案

您正在阻止主线程(嗯,而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];
}

关于ios - iOS:如何刷新功能中的 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14158316/

相关文章:

ios - DismissViewControllerAnimated EXC_Bad_ACCESS 为真

iphone - AVAudioRecorder - 最后裁剪/修剪录音

iphone - 为什么不显示 UITabBarItems 的标题?

ios - 通过检查像 SKStoreProductParameterAffiliateToken 这样的常量的地址来完成什么?

ios - iOS和S3:使用网址从S3下载图片

ios - ios中根据字段createdAt从parse中获取最新的两个对象

ios - 为什么我的 iOS 应用程序没有出现在其他应用程序的 "Open in"对话框中?

iphone - 核心图 - 自定义 x 轴标签

iOS 指向指针 NSMutableDictionary 的指针

ios - 照片和视频未保存在我的自定义相册中