I have a DatePickerView in my application (using UINavigationViewController) which I use to select date (UIDatePickerModeDate). Below is desired behavior
- When user makes a selection I save the value in NSDate member variable so that I can set that value when needed.
- 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
- User makes a selection (lets say Feb 21,2017) which sets a value to my member variable selectedDate.
- Now he spins the year wheel and while its spinning move to next screen/view.
- 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);
}