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

Dev*_*205 2 objective-c nstimer ios

我在用NSTimer,

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

然后是我的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];

}
Run Code Online (Sandbox Code Playgroud)

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

Anb*_*hik 7

如果repeats: YES];检查seconds达到或超过60,则使计时器无效并关闭VC

if (seconds >60)
{
[timer invalidate];
[self dismissViewControllerAnimated:YES completion:nil];
}
Run Code Online (Sandbox Code Playgroud)

否则一旦使用,请拨打您的计时 repeats: NO];

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

并调用函数

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

}
Run Code Online (Sandbox Code Playgroud)