Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIDatePicker date method is picking wrong date: iPhone Dev

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.

like image 458
prd Avatar asked Jun 09 '10 17:06

prd


2 Answers

The issue is finally solved. Listing what resolved the problem for me, so if someone else encounters similar issue they can try these steps

I did two things
1. set the locale and calendar of the datePicker -

NSLocale *locale = [NSLocale currentLocale];
[dateFormatter setLocale:locale];

self.datePicker.locale = locale; 
self.datePicker.calendar = [locale objectForKey:NSLocaleCalendar];

But this alone did not solve the problem.
2. I put 'self' before each reference to datePicker i.e. replaced all datePicker with self.datePicker. This must have forced the compiler setters and getters to be called.

I do not understand fully on why not using 'self' caused it to behave so strangely. But yes , I will be more careful with using the correct setters for my variables.

Thank you for taking time looking at my problem.

like image 132
prd Avatar answered Oct 21 '22 11:10

prd


IBOUTLET UIDatePicker *datePicker;
IBOUTLET UILabel *label;
}
-(void)click:(id)sender;


- (void)viewDidLoad {

    [super viewDidLoad];
    NSDate *dateFromPicker = [[NSDate alloc]init];
    [date setDate:dat animated:YES];
}
-(void)click:(id)sender {

    NSDate *selectDate = [datePicker date];
    NSString *dateString = [NSString stringWithFormat:@"%@",selectDate];

    [label setText:dateString];
}

this will work,

like image 30
Splendid Avatar answered Oct 21 '22 13:10

Splendid