Countdown Timer

前端 未结 2 1892
庸人自扰
庸人自扰 2021-01-31 11:41

I\'m trying to create a countdown timer that takes countdown, an IBOutlet connected to a textfield, from 60 seconds down to 0. I\'m not sure

A. How to limit the repeat

2条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-31 12:25

    You can add a instance variable int _timerValue to hold the timer value and then do the following. Also note that the NSTimer you are creating is already scheduled on the current run loop.

    - (IBAction)startCountdown:(id)sender
    {
        _timerValue = 60;
        [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(advanceTimer:) userInfo:nil repeats:NO];
    }
    
    - (void)advanceTimer:(NSTimer *)timer
    {
        --_timerValue;
        if(self.timerValue != 0)
           [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(advanceTimer:) userInfo:nil repeats:NO];
    
        [countdown setIntegerValue:_timerValue];
    }
    

提交回复
热议问题