Developer1205
Developer1205

Reputation: 167

Once NSTimer stop dismiss my view controller in objective c

I am using NSTimer,

timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target:self selector:@selector(updateCountdown) userInfo:nil repeats: YES];

then my 60 second count down timer program

-(void) updateCountdown {
    int hours, minutes, seconds;

   secondsLeft++;
    hours = secondsLeft / 3600;
    minutes = (secondsLeft % 3600) / 60;
    seconds = (secondsLeft %3600) % 60;


    time.text = [NSString stringWithFormat:@"%02d",  seconds];

}

my question is after 60 second dismiss my count down view controller page .. anyone can help me

Upvotes: 2

Views: 562

Answers (2)

Anbu.Karthik
Anbu.Karthik

Reputation: 82759

if repeats: YES]; check like your seconds reaches or greater than 60, invalidate your timer and dismiss your VC

if (seconds >60)
{
[timer invalidate];
[self dismissViewControllerAnimated:YES completion:nil];
}

else call your timer once using repeats: NO];

timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target:self selector:@selector(updateCountdown) userInfo:nil repeats: NO];

and call the function like

-(void) updateCountdown {
   [timer invalidate];
  [self dismissViewControllerAnimated:YES completion:nil];

}

Upvotes: 7

Lalit kumar
Lalit kumar

Reputation: 2207

timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target:self selector:@selector(updateCountdown:) userInfo:nil repeats: NO]; // set No

-(void) updateCountdown:(NSTimer *)timer
{
    [timer invalidate];
   // dismiss your controller
}

Upvotes: 2

Related Questions