Skip to content

Instantly share code, notes, and snippets.

@frijole
Created October 7, 2013 22:01
Show Gist options
  • Save frijole/6875680 to your computer and use it in GitHub Desktop.
Save frijole/6875680 to your computer and use it in GitHub Desktop.
excerpt from a uilabel subclass that counts down to a date
- (void)update
{
// check interval and update text
if ( _running ) {
// do the update
NSTimeInterval tmpInterval = [self.date timeIntervalSinceNow];
int tmpMins = tmpInterval/60;
int tmpSecs = tmpInterval-(tmpMins*60);
if ( tmpMins < 0 ) {
tmpMins = 0;
}
if ( tmpSecs < 0 ) {
tmpSecs = 0;
}
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setMinimumIntegerDigits:2];
[formatter setMaximumFractionDigits:0];
[self setText:[NSString stringWithFormat:@"%d:%@",tmpMins,[formatter stringFromNumber:[NSNumber numberWithInt:tmpSecs]]]];
[formatter release];
if ( tmpInterval > 0 ) {
[self performSelector:@selector(update) withObject:nil afterDelay:1.0];
} else {
_running = NO;
_isExpired = YES;
// completion block
if ( self.completionBlock ) {
_completionBlock();
}
}
}
}
@frijole
Copy link
Author

frijole commented Oct 7, 2013

ugh init'ing and releasing the number formatter, past me was really inefficient.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment