I'm trying to access the remaining time of an NSTimer X. I want to update the title of a button every second to reflect remaining mm:ss
until zero. I couldn't find anything here.
For example: [btY setTitle:[What to insert here?] forState:UIControlStateSelected];
Or would you rather solve this in a different way?
You can use fireDate
NSTimer *timer = [NSTimer timerWithInterval:1.0 target:self selector:@selector(updateButton) userInfo:nil repeats:YES];
- (void)updateButton:(NSTimer*)timer
{
float timeRemaining = timer.fireDate.timeIntervalSinceNow;
// Format timeRemaining into your preferred string form and
// update the button text
}
This is generally not how you would solve this.
Create a repeating NSTimer set to the resolution at which you want to update the button instead.
So for instance, if you want your button to change every second until zero, create a NSTimer like so:
NSTimer *timer = [NSTimer timerWithInterval:1.0 target:self selector:@selector(updateButton) userInfo:nil repeats:YES];
Then implement updateButton
; basically have a counter for remaining seconds, and every time updateButton
gets called, decrease the counter by one, and update the title of the button.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With