与textfield和datepicker相关的问题 [英] Issue related to textfield and datepicker

查看:122
本文介绍了与textfield和datepicker相关的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个与隐藏相关的问题并在点击文本字段时显示日期选择器视图...实际上我有2个文本字段..这是我的问题图像......

I have an issue related to hide and show the datepicker view when click on textfield...Actually I have 2 textfields..Here is my problem image...

问题

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

datepicker show and hide when click on textfiled...it should show on begin editing and hide on end editing...

第一次点击textfield 1时它的工作正常,但是只要它 resignFirstResponder 并让第二个字段响应,问题发生了。

At 1st time when we click on "textfield 1" its working good, but as soon as it resignFirstResponder and make 2nd field to respond, the problem occurs.

代码

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 to use date picker as input 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;
}

这篇关于与textfield和datepicker相关的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆