计算在多个视图控制器ios上花费的时间
我有一个iOS应用程序,为有时间限制的用户提供测试 测试将跨越多个视图控制器,这些视图控制器可能在测试流期间重新打开。我想到以下流程: 在计算在多个视图控制器ios上花费的时间,ios,objective-c,Ios,Objective C,我有一个iOS应用程序,为有时间限制的用户提供测试 测试将跨越多个视图控制器,这些视图控制器可能在测试流期间重新打开。我想到以下流程: 在AppDelegate.h中,添加一个NSTimer和在float中花费的时间: @property (strong, nonatomic) NSTimer *timer; @property (nonatomic) float timeSpent; - (void)startTimer; - (void)stopTimer; 在不忘记上述内容的情况下,在
AppDelegate.h
中,添加一个NSTimer
和在float
中花费的时间:
@property (strong, nonatomic) NSTimer *timer;
@property (nonatomic) float timeSpent;
- (void)startTimer;
- (void)stopTimer;
在不忘记上述内容的情况下,在AppDelegate.m
中创建启动和停止计时器功能:
- (void)startTimer {
self.timer = [NSTimer scheduledTimerWithTimeInterval: 0.1f target: self
selector: @selector(updateTime) userInfo: nil repeats: YES];
}
- (void)stopTimer {
[self.timer invalidate];
}
在定期调用的updateTime
函数中,将timeSpent
的值增加1
最后,在每个视图控制器中,获取timeSpent
值,并使用我想要的格式对其进行格式化,例如“01min 56s”或“01:56”
有没有更简单的方法
注:
- 没有可用的互联网连接,测试将持续约10分钟;因此,使用谷歌分析是一种过分的做法&在这种情况下不适用
@interface
@property (nonatomic, strong) NSTimer *myTimer;
@end
@implementation MySingletonClass
+ (instancetype)shared
{
static MySingletonClass *_shared = nil;
static dispatch_once_t oncePredicate;
dispatch_once(&oncePredicate, ^{
_shared = [[self alloc] init];
// any other initialisation you need
});
return _shared;
}
- (void)startTimer {
self.myTimer = [NSTimer scheduledTimerWithTimeInterval: 0.1f target: self
selector: @selector(updateTime) userInfo: nil repeats: YES];
}
- (void)stopTimer {
[self.myTimer invalidate];
}
然后您可以像这样从程序中的任何其他类访问它
#import "MySingletonClass.h"
//some method
- (void)myMethod
{
CGFloat currentTime = [MySingletonClass shared].globalTimeProperty;
// do something with the time
}
-(void)startTimer
{
[[MySingletonClass shared] startTimer];
}
-(void)updateTime
{
// do your update stuff here
}
单重头
@interface
@property (nonatomic, assign) CGFloat globalTimeProperty;
+ (instancetype)shared;
- (void)startTimer;
- (void)stopTimer;
@end
我可能错过了一些东西,但它应该足以让您继续。您想跟踪特定的viewcontroller时间还是每个viewcontroller?跟踪跨多个视图控制器花费的时间。然后您跟踪了不同视图控制器的差异变量中的时间吗?只有1个计时器(和1个
timespunt
variable`)将用于跟踪时间。相同的变量。。。请提供样本代码。即使是短的也很好。