Aleks N.
Aleks N.

Reputation: 6247

UIDatePicker vs. UIApplicationSignificantTimeChangeNotification

I'm handling UIApplicationSignificantTimeChangeNotification in my app and all the other screens in the app are gracefully refreshed when midnight comes. The one I have trouble with is with UIDatePicker. UIPickerView has refreshAllComponents method which I use in another screen to make an update on midnight change. I would like to have the same for UIDatePicker, but unfortunately I couldn't find a way to refresh it, and Today remains Today, although it is already Yesterday. Any way out?

Upvotes: 2

Views: 1071

Answers (3)

Zoleas
Zoleas

Reputation: 4879

This is a pretty old topic, but as I found it with google, so will other people. What worked for me :

[datePicker becomeFirstResponder];
[datePicker reloadInputViews]; 

Upvotes: 0

benzado
benzado

Reputation: 84338

Why not replace the UIDatePicker with new instance?

UIDatePicker *oldPicker = self.myDatePicker;
UIDatePicker *newPicker = [[UIDatePicker alloc] initWithFrame:oldPicker.frame];
[newPicker setDate:oldPicker.date animated:NO];
[[oldPicker superview] addSubview:newPicker];
[oldPicker removeFromSuperview];
self.myDatePicker = newPicker;
[newPicker release];

The above code will swap oldPicker for newPicker, copying settings so no one will notice the change. If you have other references to the picker view, you'll need to update them, too.

Upvotes: 2

Massimo Cafaro
Massimo Cafaro

Reputation: 25419

You can refresh the UIDatePicker using its method

- (void)setDate:(NSDate *)date animated:(BOOL)animated

Simply pass as the date argument [NSDate date] and YES as the animated argument. This should work since after midnight [NSDate date] must produce the correct current date.

Upvotes: 1

Related Questions