Cannot set date on UIDatePicker

I have a DatePickerView in my application (using UINavigationViewController) which I use to select date (UIDatePickerModeDate). Below is desired behavior

  1. When user makes a selection I save the value in NSDate member variable so that I can set that value when needed.
  2. Once the user makes a selection he can move to next view. If the user comes back to datepicker view then UIDatePicker should show previous selection.

Now I am testing following situation

  1. User makes a selection (lets say Feb 21,2017) which sets a value to my member variable selectedDate.
  2. Now he spins the year wheel and while its spinning move to next screen/view.
  3. When user navigates back to DatePicker view, I set the UIDatePicker date to Feb 21, 2017 (in viewWillAppear) which is previous selection but I see a different value. It seems that I am seeing the value that would appear if I would have waited until the wheel would have stop spinning in step 2.

I don't know what am I missing. All I want is to set a value on UIDatePicker and looks like there is not way I can do that. Any help is appreciated. Thanks


Here is code


- (void)viewDidLoad {
    [super viewDidLoad];
    [self.datePicker addTarget:self action:@selector(onWheelSpin) forControlEvents:UIControlEventValueChanged];
}
-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if(self.selectedDate) {
        NSLog(@"Setting date to %@",self.selectedDate.description);
        [self.datePicker setDate:self.selectedDate];
    } else {
        [self.datePicker setDate:[NSDate date]];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
- (void) onWheelSpin {
    NSLog(@"Date Picker has been rolled.");
    [self setSelectedDate:[self.datePicker date]];
    NSLog(@"Selected date = %@",self.datePicker.date.description);
}

Even though the view has advanced to the next screen the date picker is still spinning, stops and resets the value of selectedDate to a new value. To solve this create a Boolean that indicates that the date picker is being displayed. When the user advances to the next screen set that Boolean to 'no'. When the viewWillAppear set that boolean to yes. Test the boolean before resetting selectedDate.

I don't see the behavoir that you have stated. In my case onWheelSpin() doesn't get called once the view advances even though the wheel is spinning. I have logs for viewWillAppear and viewWillDisapper and I do see the logs but onWheelSpin() never gets called in this case. To me looks like UIDatePicker is broken as we cannot set a predefined date. If I set date returned by [NSDate date] in viewWillAppear() it works fine but if I set selectedDate it doesn't.

So you are now saying that your problem is that you can't set the date in the date picker programmatically to anything other than 'now' not that the problem is that when a user changes views while the date picker is moving it registers incorrectly. I believe 'now' is the default and you are not setting the date picker like you think you are. Good luck!

Cannot set date on UIDatePicker
 
 
Q