继续使用不同类中的 NSTimer
我正在制作一个有测试的应用程序。我犯了一个错误,就是为每个问题设置不同的类,比如 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将计时器从问题组类别重新定位到应逻辑管理计时器的任何类别。现在重构代码似乎工作量太大,但如果让此类问题恶化,痛苦只会变得更糟。
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.
就您而言计时器似乎是大多数文件中的可共享资源因此您会考虑将其放在单独的类中,并且可以由那里的任何文件(类)访问,那么为此你需要使用单例类设计模式,
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,