继续使用不同类中的 NSTimer

发布于 2024-11-19 22:04:53 字数 510 浏览 2 评论 0原文

我正在制作一个有测试的应用程序。我犯了一个错误,就是为每个问题设置不同的类,比如 10 个问题,然后将它们分组。现在我在第一堂课中有一个可以工作的秒表,但显然当我去下一堂课时它就停止了(10个问题),我如何从新班级访问另一个班级中的 NSTimer 并继续计时器。这是我的计时器代码:

 - (void)viewDidLoad {

gameTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(gameTimerVoid) userInfo:nil repeats:YES];

[super viewDidLoad];

}
 -(void)gameTimerVoid {

int now;
now = [gameTime.text intValue];
int after;
after = now + 1;
gameTime.text = [NSString stringWithFormat:@"%i", after];

  }

I am making an app where there is a test. I made a mistake of having different classes for each like 10 questions, to group them. Now I have a working stopwatch in the first class, but obviously it stops when i go to the next class (10 questions), how can I access the NSTimer in the other class from the new class and CONTINUE the timer. Here is my timer code:

 - (void)viewDidLoad {

gameTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(gameTimerVoid) userInfo:nil repeats:YES];

[super viewDidLoad];

}
 -(void)gameTimerVoid {

int now;
now = [gameTime.text intValue];
int after;
after = now + 1;
gameTime.text = [NSString stringWithFormat:@"%i", after];

  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

喜爱皱眉﹌ 2024-11-26 22:04:54

将计时器从问题组类别重新定位到应逻辑管理计时器的任何类别。现在重构代码似乎工作量太大,但如果让此类问题恶化,痛苦只会变得更糟。

Relocate the timer from your question group class to whatever class should logically manage the timer. It may seem like too much work to refactor your code now, but the pain will only get worse if you let this sort of problem fester.

生生漫 2024-11-26 22:04:53

就您而言计时器似乎是大多数文件中的可共享资源因此您会考虑将其放在单独的类中,并且可以由那里的任何文件(类)访问,那么为此你需要使用单例类设计模式

In your case the timer seems the sharable assets across most of your files So you would consider having it in the separate class and could be accessed by any files (class) from there,then for doing such you need to use the singleton class design pattern,

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文