gpt4 book ai didi

iphone - 无法更新 UILabel 中的文本

转载 作者:行者123 更新时间:2023-12-03 16:17:44 24 4
gpt4 key购买 nike

我已经从 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com