我希望在启动app时恢复时间计数器值。首先我的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;
}
感谢提前..
答案 0 :(得分:1)
在nsuserdefault中尝试此code.store数据
- (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"];
答案 1 :(得分:0)
在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。
干杯享受肯定会为你工作..