UIDatePicker date method is picking wrong date: iPhone Dev

Posted by prd on Stack Overflow See other posts from Stack Overflow or by prd
Published on 2010-06-09T17:43:40Z Indexed on 2010/06/09 22:52 UTC
Read the original article Hit count: 301

Filed under:
|

Hi, I am getting very strange behaviour on UIDatePicker. I have a view with date picker declared in .h file as IBOutlet UIDatePicker *datePicker; with property nonatomic and retain. datePicker is properly linked in IB file.

In the code I am setting the minimum, maximum, initial date and action to call for UICOntrolEventValueChanged using following code

If (!currentDate) {
    initialDate = [NSDate date];
} else {
    initialDate = currentdate;
}

[datePicker setMinimumDate:[NSDate date]];
[datePicker setMaximumDate:[[NSDate date] addTimeInterval:5 * 365.25 * 24 * 60 * 60]]; // to get upto 5 years 
[datePicker setDate:initialDate animated:YES];

[datePicker addTarget:self action:@selector(getDatePickerValue:) forControlEvents:UIControlEventValueChanged];

In getDatePickerValue, I get the new date using datePicker.date.

When the view is closed (using a done button), I get the current value of the date using datePicker.date.

Now if the view is called with no 'currentDate', the picker returns 'todays date'. This is what happens the 'first' time my pickerView is called. Each subsequent call to the view, with no 'current date' gives me a different and later date from today. So,

first time I get today's date say 9 Jun 2010
second time datePicker.date returns 10 Jun 2010
third time 11 Jun 2010 and so on. Though its not always incremental, but mostly it is.

I have put NSLogs, and verified the initial date is set correctly.

The problem is only on the device (on OS 3.0), the issue is not replicated on simulator.

I can't find what I have done wrong. I hope somebody else has come across similar problem and can help me resolve this.

© Stack Overflow or respective owner

Related posts about iphone

Related posts about uidatepicker