iphone - 无法更新 UILabel 中的文本

标签 iphone cocoa cocoa-touch

我已经从 UIViewController 创建了我的 ViewController,其中添加 UILabel 作为; @property(非原子,保留)UILabel* ProgressLabel;

我在loadView中初始化这个标签

- (void)loadView {

 // Set main view to View Controller
 UIView* localView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] ];
 self.view = localView;
 [localView release];

 // Set progress label
 self.progressLabel =  [[UILabel alloc] initWithFrame:CGRectMake(0, 420, 320, 20) ];
 [self.progressLabel setFont:[UIFont fontWithName:@"Georgia" size:12]]; 
 [self.progressLabel setTextColor:[UIColor whiteColor]];
 [self.progressLabel setBackgroundColor:[UIColor blackColor]];
 [self.view addSubview:self.progressLabel];

}

我有一个必须更新标签文本的方法

-(void) doSomethig {
 for(int i = 0; i < 10; i++) {
  NSString* str = [NSString stringWithFormat:@"%d", i];
  [self.progressLabel setText:str];
  [self.progressLabel.superview setNeedsDisplay];
  NSLog(@"%@", self.progressLabel.text);
  sleep(1);
 }
}

为什么这段代码不更新progressLabel.text属性?但是在调试器控制台中我看到以下文本:

2010-02-26 14:21:55.707 iLabelUpdate[6272:207] 0
2010-02-26 14:21:56.708 iLabelUpdate[6272:207] 1
2010-02-26 14:21:57.708 iLabelUpdate[6272:207] 2
2010-02-26 14:21:58.709 iLabelUpdate[6272:207] 3
2010-02-26 14:21:59.709 iLabelUpdate[6272:207] 4
2010-02-26 14:22:00.710 iLabelUpdate[6272:207] 5
2010-02-26 14:22:01.710 iLabelUpdate[6272:207] 6
2010-02-26 14:22:02.711 iLabelUpdate[6272:207] 7
2010-02-26 14:22:03.711 iLabelUpdate[6272:207] 8
2010-02-26 14:22:04.711 iLabelUpdate[6272:207] 9

当周期结束时,我可以看到进度标签中的“9”?

最佳答案

运行循环更新 UI。您可能想尝试在 for 循环中调用 runloop:

[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.01]];

关于iphone - 无法更新 UILabel 中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2341491/

相关文章:

iphone - 在 iOS 上播放 MP3 或 AAC 文件有哪些音频播放选项?

iphone - iphone模拟器上的sqlite3数据库存储在哪里?

html - 在 iphone 中被剪切的文本

ios - 架构 armv7 的 undefined symbol ,ld : symbol(s) not found for architecture armv7 - what to do?

ios - 动画 CALayer 边框 alpha

ios - 你能以编程方式接受 UITextView 中的拼写更正吗?

iphone - 如何更改 entitlements.plist 的路径

cocoa - NSFileManager 使用 NSURL 不一致的原因是什么?

cocoa - 对图层支持的 View 进行动画处理时缩放图层内容

cocoa - 在 Mojave 上禁用 NSScrollView 内部的活力?