I have a timer class set up that is basically handling all of count down timer logic. All it does is on button press – counts from 60 to 0 seconds.
I have the following code in a Timer.m class.
- (void)advanceTimer
{
self.lengthOfTime = [NSNumber numberWithInt:self.lengthOfTime.intValue - 1];
NSLog(@"%@",self.lengthOfTime);
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(advanceTimer) userInfo:nil repeats:NO];
}
- (void)startCountdown
{
if (!self.lengthOfTime) self.lengthOfTime = [NSNumber numberWithInt:60];
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(advanceTimer) userInfo:nil repeats:NO];
}
What I am looking to do is create a timer object in my View Controller that will update a label from the ViewController.m. Currently – the class works because I can NSLog from the Timer class and it counts down correctly. I thought about having the advanceTimer method return – but I can’t seem to wrap my head around how to update the label in the ViewController with the returned data.
The only way I cold get the return to work was to have a button that refreshed the label to the correct countdown time… I can’t get it to automatically count down…
Well, if you know how to update a label by clicking a button, you have everything in place to connect everything else:
If your view controller has an IBOutlet for the label and an IBAction that updates it, why not call the view-controller’s action in your
advanceTimermethod?Yet easier, you could connect your timer class to the label.
You might do it like this:
This code has a two benefits: