I added two new properties, one for my timer, and one to track when the timer was started.
New method declarations
I added an action for choosing the button, as well as a callback for the timer.
- (void) updateTime:(NSTimer*)timer
- (IBAction) startTimer:(id) sender;
Interface builder changesI added a label and placed a custom button to intercept actions to the label. I could have actually implemented touches, but this was easier than a gesture controller. I then hooked my button the callback to start the timer.
The implementation of the timer start
Toggle the timer. Check to see if it is running, if it is then reset the label, and release the timer (by setting the property to null which causes it to be released.
- (void) startTimer:(id) sender
self.timer=[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(updateTime:) userInfo:nil repeats:YES];
The callback for counting down
Note the use of floor, without using floor it instantly goes down to 59 when the timer starts, and will reach the end early.
NSDate *current=[[NSDate alloc]init];
int timeLeft=60-floor([current timeIntervalSinceDate:timerStart]);
[self.timerLabel setText:[NSString stringWithFormat:@":%0d",timeLeft]];