On Jun 11, 2005, at 1:58 PM, Jerry Brace wrote: I'm using an NSTimer for an event which fires once after it is set (Repeats:NO).
I'd like to know how to cancel/remove this timer in another event.
Ex: I click "Snooze" to start the timer and then I click the "Close" before the snooze timer fires. I want the close button to kill the snooze timer.
NSTimer * snoozeTimer = [NSTimer timerWithTimeInterval:snoozeSeconds target:self selector:@selector(snoozeControl) userInfo:nil repeats:NO]; [[NSRunLoop currentRunLoop] addTimer:snoozeTimer forMode:NSDefaultRunLoopMode];
Thanks! Jerry Make snoozeTimer an instance variable and call [snoozeTimer invalidate] to stop the timer from the clost button.
Like this:
- (id)init { self = [super init]; if (self) { // not really required snoozeTimer = nil; } return self; }
- (IBAction) snooze:(id)sender { // If we're already snoozing then do nothing if (snoozeTimer !=nil) return; // create a timer and fire it snoozeTimer = [NSTimer scheduledTimerWithTimeInterval:snoozeSeconds selector:@selector(snoozeControl:) userInfo:nil] repeats:NO]; }
- (IBAction) wakeUp:(id)sender { // if it's already stopped do nothing if (snoozeTimer == nil) return; // stop the timer [snoozeTimer invalidate];
// set it back to nil snoozeTimer = nil; }
- (void) snoozeControl:(NSTimer *)sender { // your implementation of snoozeControl here NSRunAlertPanel(@"Wake Up",@"Nap time's over",nil,nil,nil); }
Eric Brunstad Mind Sprockets Software www.mindsprockets.com |