【问题标题】:Once NSTimer stop dismiss my view controller in objective c一旦 NSTimer 停止在目标 c 中关闭我的视图控制器
【发布时间】:2017-03-27 13:15:29
【问题描述】:

我正在使用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 秒后关闭我的倒计时视图控制器页面 .. 任何人都可以帮助我

【问题讨论】:

  • NSTimer 的初始化中将 repeats: YES]; 替换为 repeats: NO];
  • 您想在 60 秒后关闭您的控制器,请明确问题
  • 是的,我想在 60 秒后关闭我的视图控制器

标签: ios objective-c nstimer


【解决方案1】:

如果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];

}

【讨论】:

  • 我无法在我的方法中访问计时器,所以如何使其无效??@Anbu.Karthick
【解决方案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
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-14
    • 1970-01-01
    • 1970-01-01
    • 2021-01-18
    • 2020-04-15
    相关资源
    最近更新 更多