Created
October 7, 2013 22:01
-
-
Save frijole/6875680 to your computer and use it in GitHub Desktop.
excerpt from a uilabel subclass that counts down to a date
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ugh init'ing and releasing the number formatter, past me was really inefficient.