简体   繁体   English

持续更新UILabel - Objective-c

[英]Update UILabel continuously — Objective-c

I'm trying to display the count of a timer in a UILabel . 我正在尝试在UILabel显示计时器的计数。 The timer works fine but I'm not able to update the label. 计时器工作正常,但我无法更新标签。 The NSTimer is set up like this in Class1 . NSTimerClass1中设置如下。

- (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;
}

In Class2 : Class2中

- (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]];
}

The correct timer value is logged but the UILabel isn't updated at all. 记录正确的计时器值,但UILabel根本没有更新。 What's wrong? 怎么了?

尽量让timerCount@property类或static

You create a new instance of the Class2 and do not present it. 您创建了Class2的新实例,但不提供它。 You should store instance of Class2 somewhere and work with it. 您应该在某处存储Class2实例并使用它。

You can simply pass the time left to the method updating the UILabel as a parameter: 您只需将剩余时间传递给更新UILabel作为参数的方法:

- (void)updateTimeLeftLabel:(NSInteger)timeLeft {
    timeLeftLabel.text = [NSString stringWithFormat:@"Time left: %i",
                          timeLeft];
}

- (void)timerDecrement {
    Class2 *cls2 = [[Class2 alloc] init];
    timerCount = timerCount-1;
    [cls2 updateTimeLeftLabel:timerCount];
}

I needed to update count on my UIButton so i have done like this 我需要更新我的UIButton计数,所以我这样做了

    -(void)viewDidLoad {
       self.myLeagueTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateCountdown) userInfo:nil repeats: YES];
    }

- (int)timeIndervalRemain {
    NSDate *currentTime = [NSDate date];
    return (TIME_INTERVAL - [currentTime timeIntervalSinceDate:self.startTime]);
}

-(void) updateCountdown {

    if ([self timeIndervalRemain] > 0) {
        self.title = [NSString stringWithFormat:@"%d", [self timeIndervalRemain]];
    }  else {
        [self timeExpire];
    }
}


-(void) timeExpire {

    if (_myLeagueTimer) {
        [_myLeagueTimer invalidate];
        _myLeagueTimer = nil;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM