LvN LvN - 4 months ago 24
Objective-C Question

How to use single IOS UIDatePicker object for multiple UITextfields

I have following three UITextfields associated with UIDatePickers.

self.taskDate.inputView=[self returnDatePickerView:01];
self.taskStartTime.inputView=[self returnDatePickerView:02];
self.taskEndtime.inputView=[self returnDatePickerView:03];


This is my DatePicker method

- (UIView *)returnDatePickerView:(int)tag{
UIToolbar *toolBar1 = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
toolBar1.barStyle = UIBarStyleBlackTranslucent;
UIBarButtonItem *doneButton3 = [[UIBarButtonItem alloc] initWithTitle:@"Done"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(taskDateDone)];
[toolBar1 setItems:[NSArray arrayWithObjects:doneButton3, nil]];
UIView *dateView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 200)];
datePicker = [[UIDatePicker alloc] initWithFrame:pickerFrame];
if(tag==01){
datePicker.datePickerMode=UIDatePickerModeDate;
}else{
datePicker.datePickerMode=UIDatePickerModeTime;
}
//Setting user selected date as the date picker default
[datePicker setDate:taskDate animated:YES];
[datePicker addTarget:self action:@selector(taskDatePicked:)forControlEvents:UIControlEventValueChanged];
[dateView addSubview:datePicker];
[dateView addSubview:toolBar1];
return dateView;
}


Here is my call back taskDatePicked

- (void)taskDatePicked:(id)sender
{
NSDateFormatter *systemtimeFormatter = [[NSDateFormatter alloc] init];
NSLog(@"tag %ld",textFieldTag);
//[systemtimeFormatter setTimeZone:[NSTimeZone systemTimeZone]];
if(textFieldTag==01){
taskDate = datePicker.date;
[systemtimeFormatter setDateStyle:NSDateFormatterShortStyle];
self.taskDate.text=[[NSString alloc] initWithString:[systemtimeFormatter stringFromDate:taskDate]];
}else if(textFieldTag==02){
taskStartTime = datePicker.date;
[systemtimeFormatter setTimeStyle:NSDateFormatterShortStyle];
self.taskStartTime.text=[[NSString alloc] initWithString:[systemtimeFormatter stringFromDate:taskStartTime]];
NSLog(@"taskStartTime picked date %@",taskStartTime.description);
}else if(textFieldTag==03){
taskEndTime = datePicker.date;
[systemtimeFormatter setTimeStyle:NSDateFormatterShortStyle];
self.taskEndtime.text=[[NSString alloc] initWithString:[systemtimeFormatter stringFromDate:taskEndTime]];
}
[datePicker reloadInputViews];
}


But this method is not calling for all UITextfieds.
What i did wrong here. Please suggest

Answer

Try the following code for using single date picker as input view for multiple text fields with different date/time formats.

Here input views for the text fields are assigned in the text field delegate.

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField

Consider UIDatePicker *datePicker; is the common date picker and NSInteger textFieldTag; holds the tag of the current text field (Both added in the interface extention).

Make sure tag value is assigned for the text fields and delegate is connected for each fields.Then add the following in the viewDidLoad method.

datePicker = [[UIDatePicker alloc] init];
[datePicker addTarget:self action:@selector(taskDatePicked:) forControlEvents:UIControlEventValueChanged];

Add the text field delegate like :

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    switch (textField.tag)
    {
        case 1:
            datePicker.datePickerMode = UIDatePickerModeDate;
            break;
        case 2:
            datePicker.datePickerMode = UIDatePickerModeTime;
            break;
        case 3:
            datePicker.datePickerMode = UIDatePickerModeTime;
            break;
        default:
            break;
    }
    textField.inputView = datePicker;
    textFieldTag = textField.tag;
    return YES;
}

And the event triggered with the date picker changes is as follows:

- (void)taskDatePicked:(id)sender
{

    UITextField *textField = (UITextField *)[self.view viewWithTag:textFieldTag];
    NSDateFormatter *dateformatter = [[NSDateFormatter alloc]init];
    dateformatter.dateStyle        = NSDateFormatterMediumStyle;
    switch (textFieldTag)
    {
        case 1:
            [dateformatter setDateFormat:@"dd-MM-YYYY"];
            break;
        case 2:
            [dateformatter setDateFormat:@"hh:mm a"];
            break;
        case 3:
            [dateformatter setDateFormat:@"hh:mm a"];
            break;
        default:
            break;
    }
    textField.text = [dateformatter stringFromDate:[datePicker date]];
}