Sure, good to have that clear.
Then yes, they are indeed pointers to timers. The timers are created inside their methods, I use those pointers to reference them and use invalidate.
Here is part of the code:
Code:
.h (declaration of timers)
@interface ATimerViewController : UIViewController {
IBOutlet UIDatePicker *timePicker;
NSTimer *myTimer;
NSTimer *newTimer;
// 1st Timer
- (IBAction) echoTime: (id) sender;
- (void) echoIt: (NSTimer *) timer;
// 2nd TIMER
- (IBAction) newActionTimer: (id)sender;
- (void) echoNewTimer: (NSTimer *) timer2;
.m (method for 2nd Timer , the other is similar)
Code:
---------------- 2nd Timer -----------------
- (IBAction) newActionTimer: (id)sender
{
[newTimer invalidate];
[newTimer release];
NSDate * time = timePicker.date;
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"HH:MM:SS"];
NSLog(@"date:%@", [dateFormatter stringFromDate:time]);
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comps = [gregorian components:(NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:time];
NSInteger hour = [comps hour];
NSInteger minute = [comps minute];
NSLog(@"Hour:%i", hour);
NSLog(@"minute:%i", minute);
NSInteger secs =hour * 60 * 60 + minute * 60;
NSNumber *elapsedSeconds = [NSNumber numberWithInt:secs];
NSDictionary *myDict2 = [NSDictionary dictionaryWithObject:elapsedSeconds forKey:@"TotalSeconds"];
newTimer = [NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:@selector(echoNewTimer:)
userInfo:myDict2
repeats:YES];
[newTimer retain];
}
- (void) echoNewTimer: (NSTimer *)timer2
{
NSNumber *num = (NSNumber *) [[timer2 userInfo] valueForKey:@"TotalSeconds"]; //elapsed
seconds++;
NSInteger secs2 = [num integerValue] - seconds; //remaining
NSLog(@"elapsed: %i, remaining: %i", seconds, secs2);
if (secs2 <= 0)
{
[timer2 invalidate];
timer2 = nil;
}
label4.text = [NSString stringWithFormat:@"NEW TIMER: %i", secs2];
}
After that I implement a Cancel method pointing to sender (button)
So, my goal is to use 1 start button and 1 cancel button.. and just do their actions. I have set up a the start button to start both timers, obviously both start their countdown at the same time which is not good.
I want to tell one timer to start and if I press cancel, invalidate it. Then If I press start again, call the second timer. (I do this because I read that you can't reuse a timer after you invalidate it).
Some people have suggested to use Booleans like true or false, or conditions. What do you think?