NSTimer Reset Not Working
Posted
by user355900
on Stack Overflow
See other posts from Stack Overflow
or by user355900
Published on 2010-06-01T22:00:40Z
Indexed on
2010/06/01
22:03 UTC
Read the original article
Hit count: 259
hi, i have a nstimer and it works perfectly counting down from 2:00 but when i hit the reset button it does not work it just stops the timer and when i press start again it will carry on with the timer as if it had never been stopped. Here is my code `@implementation TimerAppDelegate
@synthesize window;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
timerLabel.text = @"2:00"; seconds = 120; // Override point for customization after application launch [window makeKeyAndVisible]; } (void)viewDidLoad { [timer invalidate]; }
(void)countDownOneSecond { seconds--;
int currentTime = [timerLabel.text intValue]; int newTime = currentTime - 1; int displaySeconds = !(seconds % 60) ? 0 : seconds < 60 ? seconds : seconds - 60; int displayMinutes = floor(seconds / 60);
NSString *time = [NSString stringWithFormat:@"%d:%@%d", displayMinutes, [[NSString stringWithFormat:@"%d", displaySeconds] length] == 1 ? @"0" : @"", displaySeconds ]; timerLabel.text = time; if(seconds == 0) { [timer invalidate]; } }
(void)startOrStopTimer { if(timerIsRunning){ [timer invalidate]; [startOrStopButton setTitle:@"Start" forState:UIControlStateNormal]; } else { timer = [[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countDownOneSecond) userInfo:nil repeats:YES] retain]; [startOrStopButton setTitle:@"Stop" forState:UIControlStateNormal]; }
timerIsRunning = !timerIsRunning; }
(void)resetTimer { [timer invalidate]; [startOrStopButton setTitle:@"Start" forState:UIControlStateNormal]; [timer invalidate]; timerLabel.text = @"2:00"; }
- (void)dealloc { [window release]; [super dealloc]; }
@end` thanks
© Stack Overflow or respective owner