Hello again, another problem that seemed simple but is driving me bananas.
I have a timer that counts down -1 over a number of views. It works fine the first time, but if I return to the first view, the timer counts down -2. if I do this a third time it counts down -3 and so on.
I need to know how to reset the timer.
I think I have to use invalidate and an if statement, but I just can't work out where to put it?
Any advice?
Here is my .h
Code:
#import <UIKit/UIKit.h>
#import "PicPuzzleAppDelegate.h"
#import "puzzle2.h"
@interface puzzle1 : UIViewController {
IBOutlet UILabel *theScore;
IBOutlet UILabel *theTime;
NSInteger counter;
}
@property (retain, nonatomic) UILabel *theScore;
@property (retain, nonatomic) UILabel *theTime;
@property (nonatomic, readwrite) NSInteger counter;
-(IBAction)correct1;
-(IBAction)HomeButton;
-(IBAction)pushforward;
@end
and here is part of my .m code ( I think the problem lies in this section)
Code:
- (void)viewDidLoad
{ //CHECK TIMer?
PicPuzzleAppDelegate *PicPuzzle = (PicPuzzleAppDelegate *)[[UIApplication sharedApplication]delegate];
//set timer
PicPuzzle.minutes = 5.00;
PicPuzzle.seconds = 0.00;
// Start the timer for the countdown
PicPuzzle.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countDown) userInfo:nil repeats:YES];
//set score to 0
PicPuzzle.data = 0;
[super viewDidLoad];
}
-(void)countDown
{
PicPuzzleAppDelegate *PicPuzzle = (PicPuzzleAppDelegate *)[[UIApplication sharedApplication]delegate];
// Question live counter
{
theTime.text = [NSString stringWithFormat:@"%i:%2i",PicPuzzle.minutes, PicPuzzle.seconds];
if (PicPuzzle.seconds >= 1) {
PicPuzzle.seconds = PicPuzzle.seconds - 1;
}
else {
PicPuzzle.minutes = PicPuzzle.minutes - 1;
PicPuzzle.seconds = 59;
}
}
if(PicPuzzle.minutes == 0 && PicPuzzle.seconds ==0)
{
puzzleEnd *screen = [[puzzleEnd alloc] initWithNibName:nil bundle:nil];
screen.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:screen animated:YES];
[screen release];
}
// [self pushforward];}
}
I've tried putting:
Code:
/* if(PicPuzzle.timer){
[PicPuzzle.timer invalidate];
PicPuzzle.timer = nil;*/
but not sure where, and it won't work.
Can someone show me what I'm doing wrong?
Cheers guys.
Marshu