1

I have a NSDatePicker in graphical mode, and I set it up this code is called every second to update the current time in NSDatePicker object (datePicker).

- (void) updateTimerNoOne:(NSTimer *) timer {
    [datePicker setDateValue: [NSDate date]];
    [datePicker setNeedsDisplay: YES];
}

enter image description here --> reset when updateTimerNoOne is called Selected date is reset when the method is called

The problem is that when I select some date, this function always reset so that the selected date is gone.

How can I keep the selected date?

prosseek
  • 182,215
  • 215
  • 566
  • 871

1 Answers1

0

The method that is called every second should have been modified from

[datePicker setDateValue: [NSDate date]];

To

[datePicker setDateValue: [datePicker dateValue]];

To keep the selected date as highlighted. Make a new NSDate by combining two NSDates

The problem is that the dateValue's hour/minute/second is not changed, so I needed this code for updating them.

unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit;
NSDateComponents *comps = [[NSCalendar currentCalendar] components:unitFlags fromDate:[datePicker dateValue]];
NSDate *day3 = [[NSCalendar currentCalendar] dateFromComponents:comps];
unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
comps = [[NSCalendar currentCalendar] components:unitFlags fromDate:[NSDate date]];
day3 = [[NSCalendar currentCalendar] dateByAddingComponents:comps toDate:day3 options:0];
Community
  • 1
  • 1
prosseek
  • 182,215
  • 215
  • 566
  • 871