axel
axel

Reputation: 422

Nstimer Implementation

I searched all threads but not one answer was really clear to me. So, i want to create an nstimer which will retrieve the integer from the user in minutes and starts the countdown with seconds. I want if the user place 15 the timer to start counting in this format 14:59, 14:58. I implement the following code for seconds. I mean that user gives an integer in seconds and starts the counter.

setTime -= 1;
minutes = setTime /60;
seconds = setTime - (minutes *60);
NSString * timeroutput = [NSString stringWithFormat:@"%2d:%.2d", minutes, seconds];

So i tried the following code but i can not countdown in seconds. Any suggestions please?

minutes = setTime;
seconds = (setTime * 60) % 60;
NSString * timeroutput = [NSString stringWithFormat:@"%2d:%.2d", minutes, seconds];

My timer sets with the following code:

-(void)setTimer{
Timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerRun) userInfo:nil repeats:YES];
}

In (timerRun) method i implement the time.

Upvotes: 0

Views: 181

Answers (1)

zzyzy
zzyzy

Reputation: 983

NSInteger seconds;
NSInteger userMinutes;
NSTimer *myTimer;

-(void)setTimer
{
    seconds = userMinutes*60;
    myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                             target:self
                                           selector:@selector(timerRun)
                                           userInfo:nil
                                            repeats:YES];
}

-(void)timerRun
{
    if (seconds-- > 0)
        NSString *timerOutput = [NSString stringWithFormat:@"%02ld:%02ld", seconds/60, seconds%60];
    else
        [myTimer invalidate];
}

Upvotes: 3

Related Questions