我正在尝试在UILabel
中显示计时器的计数。计时器工作正常,但我无法更新标签。在 Class1 中像这样设置NSTimer
。
- (void)startTimer {
int timerCount = 5;
timer = [NSTimer scheduledTimerWithTimeInterval:4.0
target:self
selector:@selector(timerDecrement)
userInfo:nil
repeats:YES];
[timer fire];
}
- (void)timerDecrement {
Class2 *cls2 = [[Class2 alloc] init];
timerCount = timerCount-1;
[cls2 updateTimeLeftLabel];
}
+ (int)getTimerCount {
return timerCount;
}
在类别2 中:
- (void)viewDidLoad {
if (timeLeftLabel == nil) {
timeLeftLabel = [[UILabel alloc] initWithFrame:CGRectMake(200, 70, 120, 30)];
timeLeftLabel.text = @"Time left";
[self.view addSubview:timeLeftLabel];
}
- (void)updateTimeLeftLabel {
NSLog(@"%@", [NSString stringWithFormat:@"%i",
[Class1 getTimerCount]]);
timeLeftLabel.text = [NSString stringWithFormat:@"Time left: %i",
[Class1 getTimerCount]];
}
记录了正确的计时器值,但
UILabel
根本没有更新。怎么了?
最佳答案
尝试将timerCount
设置为您的类的@property
或static
。
关于ios - 持续更新UILabel-Objective-c,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20799847/