我有一个与单击文本字段时隐藏和显示日期选择器 View 相关的问题...实际上我有 2 个文本字段..这是我的问题图片...
问题
datepicker 在点击 textfiled 时显示和隐藏...它应该在开始编辑时显示并在结束编辑时隐藏...
在我们第一次点击“文本字段 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
}
When I hide keyboard after 1st textfield and then enter in second textfield its ok...otherwise the problem is as it is
最佳答案
Batter 使用日期选择器作为输入 View
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;
}
关于ios - 与文本字段和日期选择器相关的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30782211/