关闭ViewController后如何继续倒数计时器 [英] How to continue the countdown timer after closing ViewController

查看:31
本文介绍了关闭ViewController后如何继续倒数计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用UIDatePicker来实现倒数计时器.加载视图后datePickerMode设置CountDownTimer:

I use UIDatePicker to implement a countdown timer.After loading viewdatePickerMode set CountDownTimer:

- (void)viewDidLoad {
[super viewDidLoad];
self.countDownTimer.datePickerMode=UIDatePickerModeCountDownTimer;
}

首先在UIDatePicker中设置倒计时时间,然后按下按钮开始倒计时:

First,set the countdown time in UIDatePicker,then press the button to start the countdown:

- (IBAction)StartCountdown:(id)sender {
    [sender setTitle:@"Cancel" forState:UIControlStateNormal];
    countDownInterval = (NSTimeInterval)countDownTimer.countDownDuration;
    remainder = countDownInterval;
    afterRemainder = countDownInterval - remainder % 60 ;
    //call updateCountDown()
    timer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateCountDown) userInfo:nil repeats:YES];
}

调用updateCountDown():

-(void)updateCountDown{
    afterRemainder --;
    int hours = (int)(afterRemainder/(60*60));
    int mins = (int)(((int)afterRemainder/60)-(hours * 60));
    int secs = (int)(((int)afterRemainder - (60 * mins) - (60 * hours *60)));
    NSString *displayText = [[NSString alloc]initWithFormat:@"%02u : %02u : %02u",hours,mins,secs];
    self.displayLabel.text = displayText;
}

Action 可以正常执行,但是当我跳出这个视图并且不会执行 timer 为零时,我如何操作 timer 可以在跳转视图后持续?

Action can execute properly, but when I jump out of this view and will not perform timer is zero, how do I operate the timer can be sustained after the jump view?

推荐答案

我认为你可以:

  1. 当你跳出视图时,让此时的状态保存到 NSUserDefaults
    • 剩余时间,可能以秒为单位
    • 当时的当前系统时间,考试时间:13:20:15
  • 获取当前系统时间,将其与保存的系统时间(A)相结合
  • 用 (A) 子保存剩余时间(以秒为单位)并继续倒计时.

只是一个想法,但希望它有所帮助

Just a idea, but hope it helps

这篇关于关闭ViewController后如何继续倒数计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆