Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/106.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
计算在多个视图控制器ios上花费的时间_Ios_Objective C - Fatal编程技术网

计算在多个视图控制器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; 在不忘记上述内容的情况下,在

我有一个iOS应用程序,为有时间限制的用户提供测试

测试将跨越多个视图控制器,这些视图控制器可能在测试流期间重新打开。我想到以下流程:

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分钟;因此,使用谷歌分析是一种过分的做法&在这种情况下不适用

您需要一个NSTimer和变量来跟踪全局时间和。。 你的建议是,把app委托人当作荣耀的单身汉来使用,这是可行的

但请不要,这不是很好的做法。至少在我看来,这篇博文对原因做了很好的简要描述。

就个人而言,如果是我,我可能只会使用依赖项注入模型,并在ViewController之间传递NSTimer

简言之,appdelegate可能是最简单、最快捷的方法。但如果它不是一个普通的应用程序,我会建议它有一个更具伸缩性的东西

希望这有用:)

*编辑singleton的示例代码

这应该放在singleton类的顶部进行初始化

@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`)将用于跟踪时间。相同的变量。。。请提供样本代码。即使是短的也很好。