I have a UIDatePicker in my app and although I change the date selected in the simulator, when I NSLog the datePicker.date, it outputs the current date rather than the one I selected.
Here is an extract of my code: (where the picker is generated)
self.pickerView = [[UIDatePicker alloc] initWithFrame:pickerFrame];
//self.pickerView.frame = pickerFrame;
self.pickerView.datePickerMode = UIDatePickerModeDate;
actionSheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:nil
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
[actionSheet addSubview:pickerView];
UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"Done"]];
closeButton.momentary = YES;
closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
closeButton.tintColor = [UIColor blackColor];
[closeButton addTarget:self action:@selector(dismissActionSheet) forControlEvents:UIControlEventValueChanged];
[pickerView addTarget:self action:@selector(pickerChange) forControlEvents:UIControlEventValueChanged];
[actionSheet addSubview:closeButton];
[actionSheet setBounds:CGRectMake(0, 0, 320, 251)];
pickerView.maximumDate = [NSDate dateWithTimeIntervalSinceNow:315360000];
pickerView.minimumDate = [NSDate dateWithTimeIntervalSinceNow:-315360000];
Then in my pickerChange function:
-(void)pickerChange{
NSLog(@"DEBUG: Picker Value Changed");
NSLog(@"%@",self.pickerView.date );
}
The output of that NSLog is always the CURRENT DATE rather than the date that is selected in the UIDatePicker. Not sure why this is, because from what I can tell, the date property should display what is selected, not the current date (or what was originally selected prior to the user changing the value).
Any help would be appreciated. (message me if you need further info). Thanks! I’m sure it must be something I’ve overlooked whilst in a rush.
Ok. I have managed to find the answer – I’m posting it so that if any others have a similar problem, they can fix it.
I had to modify the pickerChange function to include the sender – one would expect however that any other type of reference to the datePicker would have the same effect. Here’s what it now looks like:
It seems that this was the only way to get around the problem, rather than referencing ‘pickerView’ (which was declared in the header file) you must reference the sender in this way.
Would be interesting to see if anyone else has come across this type of problem (or similar).