ios - 持续更新UILabel-Objective-c

标签 ios objective-c timer uilabel nstimer

我正在尝试在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设置为您的类的@propertystatic

关于ios - 持续更新UILabel-Objective-c,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20799847/

相关文章:

objective-c - 如何让 UIView 忽略触摸而不释放它?

objective-c - UITableView tableViewHeader 背景被切断

ios - 当 View 加载到一个 xib 文件中时选择特定的 UIView

android - 实时测量蓝牙信号强度 (RSSI)

ios - 对于长度超过其宽度的单词,将 UIButton 的标题垂直居中

iOS swift 3 : Comparing One all elements of Array in another array

ios - 如何使用具有 3 个组件的 UIPickerView 填充 UITextField?

ios - 如何在 RestKit 中映射动态响应?

c# - 继承System.Timers.Timer时替换System.Timers.Timer.Elapsed事件

使用计时器的 Javascript 幻灯片放映