1

My app has a menu, when you select the countdown clock from the menu you get delivered to the view controller. You use the datepicker to choose a date and then press the "start countdown" button. Above the datepicker is a label that displays the days, hours, mins, and secs left. I'm using this as a deployment countdown clock.

The problem that I'm having is, that after I exit the app; or after I go to a different item on the Menu, the datepicker doesn't save the date whenever I change screens or exit the app.

There is something wrong with my code, I just don't know what. Please help me, I'm new and need a good push! :)

- (IBAction)startCountdown:(id)sender {
    NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
    NSUInteger preservedComponents = (NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit);
    self.datePicker.date = [calendar dateFromComponents:[calendar components:preservedComponents fromDate:self.datePicker.date]];
    NSTimer *timer;
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                             target:self
                                           selector:@selector(updateTime)
                                           userInfo:nil
                                            repeats:YES];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSDate *selectedDate = [self.datePicker date];
    [defaults setObject:selectedDate forKey:@"DatePickerViewController.selectedDate"];
    NSDate *storedDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"DatePickerViewController.selectedDate"];
    [self.datePicker setDate:storedDate animated:NO];
}
Joshua Hart
  • 772
  • 1
  • 21
  • 31

1 Answers1

0

What I found to work was actually putting this in the -(void)viewdidload method:

NSDate *dateSelected = [[NSUserDefaults standardUserDefaults] objectForKey:@"datePicker.selectedDate"];

if (dateSelected) {
    [self.datePicker setDate:dateSelected];  
}
self.countdownTimer = [NSTimer scheduledTimerWithTimeInterval:1.0

                                                       target:self

                                                     selector:@selector(updateTime)

                                                     userInfo:nil

                                                      repeats:YES];

[self updateTime];
Joshua Hart
  • 772
  • 1
  • 21
  • 31