【问题标题】:how to resume time counter value in when i start the app如何在我启动应用程序时恢复时间计数器值
【发布时间】:2017-05-14 15:45:20
【问题描述】:

我想在启动应用程序时恢复时间计数器值。首先我的 label 值是 05:00,而不是关闭应用程序后,但是当我再次启动应用程序时,时间计时器计数开始到 05:00。请帮帮我

int timeSec,timeMin ; 
- (void)viewDidLoad {
    timeSec=0;
    timeMin=0;
    [self StartTimer];
}
-(void) StartTimer
{
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerTick:) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];


}

//Event called every time the NSTimer ticks.
- (void)timerTick:(NSTimer *)timer
{
    timeSec++;
    if (timeSec == 60)
    {
        timeSec = 0;
        timeMin++;
    }
    //Format the string 00:00
    NSString* timeNow = [NSString stringWithFormat:@"%02d:%02d", timeMin, timeSec];
    //Display on your label
    //[timeLabel setStringValue:timeNow];
    self.lbl_timer.text= timeNow;
}

//Call this to stop the timer event(could use as a 'Pause' or 'Reset')
- (void) StopTimer
{
   // [timer invalidate];
    timeSec = 0;
    timeMin = 0;
    //Since we reset here, and timerTick won't update your label again, we need to refresh it again.
    //Format the string in 00:00
    NSString* timeNow = [NSString stringWithFormat:@"%02d:%02d", timeMin, timeSec];
    //Display on your label
    // [timeLabel setStringValue:timeNow];
    self.lbl_timer.text= timeNow;
}

感谢您的提前..

【问题讨论】:

  • 将计时器开始时间保存在持久存储中的某处(例如NSUserDefaults、Core Data、SQLite 等)。然后,我们重新启动应用程序,恢复该开始时间,并将其用作显示已用时间量的基础。它是 Swift,但这说明了一些关键问题:stackoverflow.com/questions/34496389/…
  • 然后当你想重新设置你的时间?
  • 您想将计时器设置为哪个值?是应用停止时的值,还是考虑到应用会话之间的持续时间计算得出的值?

标签: ios objective-c nstimer


【解决方案1】:

试试这个code.store data in nsuserdefault

- (void)viewDidLoad {

    timeSec=[[NSString stringWithFormat:@"%ld",(long)[[NSUserDefaults       standardUserDefaults]integerForKey:@"timeSec"]] intValue];
    timeMin=[[NSString stringWithFormat:@"%ld",(long)[[NSUserDefaults standardUserDefaults]integerForKey:@"timeMin"]] intValue];
    [self StartTimer];
}
-(void) StartTimer
{
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerTick:) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
    self.lbl_timer.text=@"00:00";

}
- (void)timerTick:(NSTimer *)timer
{
    timeSec++;
    if (timeSec == 60)
    {
        [locationManager startUpdatingLocation];
        timeSec = 0;
        timeMin++;
    }
    self.lbl_timer.text= [NSString stringWithFormat:@"%02d:%02d", timeMin, timeSec];;
}

当您返回屏幕时添加此代码。例如后退按钮或关闭按钮操作

[timer invalidate];
                                       [[NSUserDefaults standardUserDefaults]setInteger:timeSec forKey:@"timeSec"];
                                       [[NSUserDefaults standardUserDefaults]setInteger:timeMin forKey:@"timeMin"];

【讨论】:

    【解决方案2】:

    在 appdelegate 文件中声明两个变量

    1)NSUserDefaults *usedefaults; 2)@property (nonatomic,readwrite) int timeSec,timeMin,isBackground;

    - (void)applicationDidEnterBackground:(UIApplication *)application 
    {
         self.isBackground=1;
         usedefaults=[NSUserDefaults standardUserDefaults];
        [usedefaults setObject:[NSString stringWithFormat:@"%d",self.timeMin] forKey:@"timemin"];
        [usedefaults setObject:[NSString stringWithFormat:@"%d",self.timeSec] forKey:@"timesec"];
        NSLog(@"Enter in back value userdefault %@",usedefaults);
    }
    
    - (void)applicationWillEnterForeground:(UIApplication *)application {
    
        if(self.isBackground==1)
        {
            self.isBackground=0;
            self.timeMin=[[usedefaults objectForKey:@"timemin"] intValue];
            self.timeSec=[[usedefaults objectForKey:@"timesec"] intValue];
            NSLog(@"Foreground value min %d sec %d",self.timeMin,self.timeSec);
        }
    }
    
    In ViewDidload in your Timer startpage
    app=(AppDelegate *)[[UIApplication sharedApplication] delegate];
    

    最后只需将你 self.timemin 替换为 app.timemin 与 timesec 相同。

    干杯享受这肯定对你有用..

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-11
      相关资源
      最近更新 更多