Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I have a similar problem. The first time I change from UIDatePickerModeDate to UIDatePickerModeTime, the time was OK. However, when I change back to the ..Date mode and then to the ..Time mode again, the time read 00:00. Thereafter, regardless of the number of mode changes, the time always remained at 00:00. I modeled my UIDatePicker programmatically the same as the UIDatePicker in Apple's UICatalog app. This behavior does not exist in their app. </p> <p>Taking your lead, I saved the NSDate value from the picker in a retained @property with the intentions of retrieving and resetting the picker upon a UIControl Notification. However, I could not find an event that fired when the picker was changed from Date mode to Time mode. I even tried something like:</p> <pre><code> [myDatePicker addTarget:self action:@selector(getDateTime:) forControlEvents:UIControlEventAllEvents]; </code></pre> <p>Of course the event fired if I changed the picker's date (or time) but as I mentioned, not when I changed the mode.</p> <p>I eventually ended up using a UISegmentedControl (same as in the UICatalog app) that targeted the same event method above where I toggled the mode. However, when I reset the picker to the saved NSDate value, the Time remained at 00:00. I then used NSLog to verify that the saved value was OK and discovered that its Time value portion had also changed to 00:00! This was indeed strange since I had not saved a new NSDate value. The original saved value simply changed on it's own! </p> <p>I'm still investigating the problem and am very close to calling it a bug, especially when a UIDatePicker is added programmatically without a supporting NIB. </p> <p>Update: My final solution was almost identical to yours. For some reason, the error continued when the saved date was stored as an NSDate value. Storing it as an NSString and typecasting both ways did the trick:</p> <pre><code>self.saveDate = (NSString*)self.myDatePicker.date; ... [self.myDatePicker setDate:(NSDate*)self.saveDate animated:NO]; </code></pre> <p>Also, since I'm retrieving and saving the date to an external device via a GCD dispatch_async block, the property, self. notation was also needed (even on the main_que). </p> <p>Final conclusion:</p> <p>I'm now convinced that setting the UIDatePicker date each and every time the picker is accessed is a necessary requirement rather than a bug. Using Apple's UICatalog app, when you set the date to today just once and then switch between Date and Time mode, their app exhibits exactly the same behavior. The date is retained but the time resets to 00:00. if you switch to a mode other than date or time and switch back, all modes will be reset to their floor values. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload