Continuing on a NSTimer in a different class
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];
}
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,
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.
精彩评论