我有一个 iPhone 应用程序,它基本上是一个时钟、秒表和计时器。一切正常,直到您离开应用程序,秒表和计时器停止计时。我认为解决这个问题的方法是在应用程序进入后台时启动一个计数器,并在应用程序再次启动时从秒表/计时器中添加/减去它。这是最好的做法吗?如果是这样,我该怎么做?
更新:计时器、秒表和时钟都在不同的 View Controller 中。这是我的秒表在 StopwatchViewController.m 中的代码:
- (void)stopwatch // Start counting the stopwatch
{
hourInt = [hourLabel.text intValue]; // Store integer values of time
minuteInt = [minuteLabel.text intValue];
secondInt = [secondLabel.text intValue];
if (secondInt == 59) { // Add on one second
secondInt = 0;
if (minuteInt == 59) {
minuteInt = 0;
if (hourInt == 23) {
hourInt = 0;
} else {
hourInt += 1;
}
} else {
minuteInt += 1;
}
} else {
secondInt += 1;
}
NSString *hourString = [NSString stringWithFormat:@"%02d", hourInt]; // Update text to show time
NSString *minuteString = [NSString stringWithFormat:@"%02d", minuteInt];
NSString *secondString = [NSString stringWithFormat:@"%02d", secondInt];
hourLabel.text = hourString;
minuteLabel.text = minuteString;
secondLabel.text = secondString;
[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(stopwatch) userInfo:nil repeats:NO]; // Repeat every second
}
如何让它添加应用程序在后台运行的时间?
最佳答案
为应用程序进入后台和应用程序进入前台添加两个观察者
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillEnterInBackGround) name:UIApplicationWillResignActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillEnterInForeground) name:UIApplicationWillEnterForegroundNotification object:nil];
- (void)applicationWillEnterInBackGround{
// stop your timer here
}
- (void)applicationWillEnterInForeground
{
// start your timer here
}
例子------
在 .h 中
NSTimer *timer;
float time;
.m
- (void)viewDidLoad
{
[super viewDidLoad];
timer=[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(stopwatch) userInfo:nil repeats:YES];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillEnterInBackGround) name:UIApplicationWillResignActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillEnterInForeground) name:UIApplicationWillEnterForegroundNotification object:nil];
}
- (void)applicationWillEnterInBackGround
{
[timer invalidate];
}
- (void)applicationWillEnterInForeground
{
timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(stopwatch) userInfo:nil repeats:YES];
}
- (void)stopwatch // Start counting the stopwatch
{
hourInt = [hourLabel.text intValue]; // Store integer values of time
minuteInt = [minuteLabel.text intValue];
secondInt = [secondLabel.text intValue];
if (secondInt == 59) { // Add on one second
secondInt = 0;
if (minuteInt == 59) {
minuteInt = 0;
if (hourInt == 23) {
hourInt = 0;
} else {
hourInt += 1;
}
} else {
minuteInt += 1;
}
} else {
secondInt += 1;
}
NSString *hourString = [NSString stringWithFormat:@"%02d", hourInt]; // Update text to show time
NSString *minuteString = [NSString stringWithFormat:@"%02d", minuteInt];
NSString *secondString = [NSString stringWithFormat:@"%02d", secondInt];
hourLabel.text = hourString;
minuteLabel.text = minuteString;
secondLabel.text = secondString;
}
关于iOS 应用程序秒表和计时器在后台停止计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20021083/