1

ラベルに表示されるカウントダウンタイマーを作成します。ユーザーがボタンを押すたびにカウントダウンタイマーに30秒追加する機能を追加したいと思います。私は自分でそれをやろうとしましたが、成功しませんでした (エラーが発生し、一度タイマーが停止しました) どうすればできますか? ここに私のコードがあります:

-(IBAction)start:(id)sender
{
    timer = [NSTimer scheduledTimerWithTimeInterval:.01 target:self     selector:@selector(updateTimer:) userInfo:nil repeats:YES];

}

-(void)updateTimer:(NSTimer *)timer
{
    currentTime -= 10 ;
    [self populateLabelwithTime:currentTime];

    if(currentTime <=0)
    {
        [timer invalidate];
        //some code when countdown reach to 00:00:00
    }

}


- (void)populateLabelwithTime:(int)milliseconds {
    seconds = milliseconds/1000;
    minutes = seconds / 60;


    seconds -= minutes * 60;

    NSString * countdownText = [NSString stringWithFormat:@"%@%02d:%02d:%02d", (milliseconds<0?@"-":@""), minutes, seconds,milliseconds%1000];
    countdownLabel.text = countdownText;

}

-(IBAction)addTime:(id)sender
{
    timer = [NSTimer scheduledTimerWithTimeInterval:.01 target:self selector:@selector(addToCurrent:) userInfo:nil repeats:YES];
}

-(void) addToCurrent:(NSTimer *)timerb;
{
    seconds +=10;
    [self populateLabelWithTime:seconds];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    currentTime = 2700;

}

ありがとう!

4

1 に答える 1

0

ボタンをクリックするたびにタイマーを再作成する必要はありません。また、タイマーに 30 秒を追加しようとすると、 currentTime の代わりに誤って秒を変更しているように見えます。

-(IBAction)addTime:(id)sender
{
    currentTime += 30000;
    [self populateLabelWithTime:currentTime];
}
于 2012-06-27T21:53:43.240 に答える