timer IBOutlet disappears. probably very simple
When I run this code, the difference between the datePicker selection and
the current real time is displayed through an IBOutlet in the form
"dd:hh:mm:ss". I have set up an IBAction button to initiate a timer
countdown from the displayed time in the IBOutlet label but when I tap on
the button to start the timer, the label that displays the timer
disappears. thanks!
@synthesize picker; @synthesize label;
-(IBAction)startTimer:(id)sender{
[NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(refreshLabel)
userInfo:nil
repeats:YES];
}
-(IBAction)displayDate:(id)sender {
NSDate *todaysDate = [NSDate date];
NSDate *selected = [picker date];
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
NSUInteger unitFlags = NSDayCalendarUnit |NSMinuteCalendarUnit |
NSHourCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *dateComparisonComponents = [gregorian
components:unitFlags
fromDate:todaysDate
toDate:selected
options:NSWrapCalendarComponents];
NSInteger days = [dateComparisonComponents day];
NSInteger hours = [dateComparisonComponents hour];
NSInteger minutes = [dateComparisonComponents minute];
NSInteger seconds = [dateComparisonComponents second];
self.label.text = [NSString
stringWithFormat:@"%ld:%02ld:%02ld:%02ld",
(long)days,
(long)hours,
(long)minutes,
(long)seconds];
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSDate *now = [NSDate date];
[picker setDate:now animated:YES];
self.label.text = [now description];
self.now = [NSDate date];
self.label.text = [self.formatter stringFromDate:self.now];
}
-(void)refreshLabel
{
NSDate *selected = [NSDate dateWithTimeIntervalSince1970:[[picker
date] timeIntervalSince1970] - 1];
[picker setDate:selected animated:YES];
self.label.text = [self.formatter stringFromDate:selected];
}
@end
No comments:
Post a Comment