与textfield和datepicker相关的问题

时间:2015-06-11 13:29:43

标签: ios iphone swift

我有一个与隐藏相关的问题并在点击文本字段时显示datepicker视图...其实我有2个文本字段..这是我的问题形象......

问题

datepicker在单击textfiled时显示和隐藏...它应该在开始编辑时显示并隐藏在结束编辑...

enter image description here

第一次点击" textfield 1"它的工作正常,但只要它resignFirstResponder并使第二个字段做出响应,就会出现问题。

代码

func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
    self.datepicker.alpha = 0.0
    self.datepicker.hidden = false
    UIView.animateWithDuration(0.3, animations: { () -> Void in
        self.datepicker.alpha = 1.0
        self.conDateHeight.constant = 100.0
        self.datepicker.addTarget(self, action: Selector("dataPickerChanged:"), forControlEvents: UIControlEvents.ValueChanged)
    })
    return true
}

func textFieldShouldEndEditing(textField: UITextField) -> Bool {
        UIView.animateWithDuration(0.3, animations: {
        self.datepicker.alpha = 0.0
        }, completion: {
            (value: Bool) in
            self.conDateHeight.constant = 0.0
            self.datepicker.hidden = true
        })
    return true
}


func textFieldShouldReturn(textField: UITextField) -> Bool {
    if textField == self.txtToDate {
        txtToDate.resignFirstResponder()
        txtFromDate.becomeFirstResponder()
    }
    else if textField == self.txtFromDate {
        txtFromDate.resignFirstResponder()
        txtToDate.becomeFirstResponder()
    }
    return true
}  
  

当我在第一个文本字段之后隐藏键盘然后在第二个文本字段中输入它确定...否则问题就是它

1 个答案:

答案 0 :(得分:1)

击球手使用日期选择器作为输入视图

UIView *viewDateInput = [[UIView alloc] initWithFrame:CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width, 200)];
[viewDateInput setBackgroundColor:[UIColor whiteColor]];
self.pickerDate = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0, viewDateInput.frame.size.width, viewDateInput.frame.size.height)];
self.pickerDate.datePickerMode = UIDatePickerModeDate;
[viewDateInput addSubview:self.pickerDate];
[self.pickerDate addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged];
[self.txtDate setInputView:viewDateInput];

日期变更功能

- (void)dateChanged:(id)sender
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *currentTime = [dateFormatter stringFromDate:self.pickerDate.date];
self.txtDate.text = currentTime;
}