如何为多个UITextfields使用单个IOS UIDatePicker对象

时间:2014-07-04 07:47:43

标签: ios objective-c ios7 uidatepicker

我有以下三个与UIDatePickers关联的UITextfield。

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

这是我的DatePicker方法

    - (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;
 }

这是我的回电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];
}

但是这种方法不是要求所有UITextfieds。 我在这里做错了什么。请建议

2 个答案:

答案 0 :(得分:1)

尝试使用以下代码将单个日期选择器用作具有不同日期/时间格式的多个文本字段的输入视图。

此处文本字段的输入视图在文本字段委托中分配。

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField

考虑UIDatePicker *datePicker;是常见日期选择器,NSInteger textFieldTag;包含当前文本字段的标记(两者都在界面扩展中添加)。

确保为文本字段指定了标记值,并为每个字段连接了委托。然后在viewDidLoad方法中添加以下内容。

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

添加文本字段委托,如:

- (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;
}

日期选择器更改触发的事件如下:

- (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]];
}

答案 1 :(得分:0)

请将标记值设置为textFieldTag

- (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];
     textFieldTag = tag;
    [dateView addSubview:datePicker];
    [dateView addSubview:toolBar1];
    return dateView;
    }