当我启动应用程序时如何恢复时间计数器值
how to resume time counter value in when i start the app
我想在我首先启动 app.like 时恢复时间计数器值 我的 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;
}
感谢提前..
在 appdelegate 文件中声明两个变量
1)NSUserDefaults *usedefaults;
2)@属性 (非原子,读写) 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 同样的时间秒。
干杯享受那肯定对你有用..
在 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];;
}
当您返回屏幕时添加此 code.like 后退按钮或关闭按钮操作
[timer invalidate];
[[NSUserDefaults standardUserDefaults]setInteger:timeSec forKey:@"timeSec"];
[[NSUserDefaults standardUserDefaults]setInteger:timeMin forKey:@"timeMin"];
我想在我首先启动 app.like 时恢复时间计数器值 我的 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;
}
感谢提前..
在 appdelegate 文件中声明两个变量
1)NSUserDefaults *usedefaults; 2)@属性 (非原子,读写) 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 同样的时间秒。
干杯享受那肯定对你有用..
在 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];;
}
当您返回屏幕时添加此 code.like 后退按钮或关闭按钮操作
[timer invalidate];
[[NSUserDefaults standardUserDefaults]setInteger:timeSec forKey:@"timeSec"];
[[NSUserDefaults standardUserDefaults]setInteger:timeMin forKey:@"timeMin"];