I am using UIDatePicker in my iPhone application.I have kept a button on clicking which the mode of datepicker switches between UIDatePickerModeTime and UIDatePickerModeDate. When I change the date of datepicker when the mode is UIDatePickerModeDate,the changed date correctly appears whe开发者_运维知识库n I switch the mode but if I change the time and switch the mode,on again switching the mode to UIDatePickerModeTime resets the time to 12:00 AM.I am not getting why this is happening and what to do for it.Please help.
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.
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:
[myDatePicker addTarget:self action:@selector(getDateTime:) forControlEvents:UIControlEventAllEvents];
Of course the event fired if I changed the picker's date (or time) but as I mentioned, not when I changed the mode.
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!
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.
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:
self.saveDate = (NSString*)self.myDatePicker.date;
...
[self.myDatePicker setDate:(NSDate*)self.saveDate animated:NO];
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).
Final conclusion:
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.
I used a Date object which I declared as a property in which I stored the date from the date picker when I present the date picker. I updated the stored date when I changes the date in date picker(Using UIControl Notification) On changing the mode of the date picker I assigned the same date to the date picker.
I understand this was fairly simple but formerly I was trying the same thing by taking a simple Date object which didn't work.Making it a property did the job for me
精彩评论