一旦NSTimer停止在目标c中关闭我的视图控制器

时间:2017-03-27 13:15:29

标签: ios objective-c nstimer

我正在使用NSTimer

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

然后是我的60秒倒数计时器程序

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

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


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

}

我的问题是在60秒后解雇我的倒计时控制器页面..任何人都可以帮助我

2 个答案:

答案 0 :(得分:7)

如果repeats: YES];检查seconds达到或大于60,请使计时器无效并解除您的VC

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

其他使用repeats: NO];

调用您的计时器
timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target:self selector:@selector(updateCountdown) userInfo:nil repeats: NO];

并调用函数

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

}

答案 1 :(得分:2)

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
}