如何从两个文本字段中获取价值?

时间:2013-01-28 08:51:56

标签: ios ios6 datepicker textfield

我有两个textFields,我已经将键盘上的默认点击行为替换为DatePicker

-(void)updateTextField:(id)sender
{
    UIDatePicker *picker = (UIDatePicker*)_editStartDate.inputView;
    _editStartDate.text = [NSString stringWithFormat:@"%@",picker.date];
}


- (void)viewDidLoad
{
   UIDatePicker *datePicker = [[UIDatePicker alloc]init];

    [datePicker setDate:[NSDate date];
    [datePicker addTarget:self action:@selector(updateTextField:) forControlEvents:UIControlEventValueChanged];       
    [_editStartDate setInputView:datePicker];
...
}

此代码创建了datepicker而不是键盘,并使用名称editStartDate更新textField,但我有第二个textField,名称为editEndDate,我不知道如何获取它的值。

你有什么想法吗?

1 个答案:

答案 0 :(得分:1)

试试这个:

-(void)updateTextField:(id)sender
{

    if([_editStartDate isFirstResponder]){
        UIDatePicker *picker = (UIDatePicker*)_editStartDate.inputView;
        _editStartDate.text = [NSString stringWithFormat:@"%@",picker.date];
    }
    if([_editEndDate isFirstResponder]){
        UIDatePicker *picker = (UIDatePicker*)_editEndDate.inputView;
        _editEndDate.text = [NSString stringWithFormat:@"%@",picker.date];
    }

}