出现“完成”按钮时出现问题,从UITextField关闭datePickerView

时间:2017-01-25 06:36:08

标签: ios swift xcode datepicker uitextfield

我有UITextField,目前正确显示DatePickerView。我已经尝试过如何添加完成按钮,但我找不到工作方式或注册以关闭DatePicker

以下是当有人点击DatePicker时显示UITextField的功能:

@IBAction func selectDate(_ sender: UITextField) {
    let datePickerView:UIDatePicker = UIDatePicker()
    datePickerView.datePickerMode = UIDatePickerMode.date
    sender.inputView = datePickerView

    datePickerView.addTarget(self, action: #selector(self.datePickerValueChanged), for: UIControlEvents.valueChanged)
}

这是一个跟踪任何更改并在UITextField

中显示的功能
func datePickerValueChanged(sender:UIDatePicker) {
    let dateFormatter = DateFormatter()
    dateFormatter.dateStyle = DateFormatter.Style.short
    dateFormatter.timeStyle = DateFormatter.Style.none

    birthday.text = dateFormatter.string(from: sender.date)

}

如何向关闭DatePicker的特定代码添加按钮?

4 个答案:

答案 0 :(得分:1)

override func viewDidLoad() {
super.viewDidLoad()

let toolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 30))

let space = UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: nil, action: nil)
let doneButton = UIBarButtonItem(title: “Done”, style: .Done, target: self, action: Selector(“doneButtonAction”))

var buttons = [UIBarButtonItem]()
buttons.append(space)
buttons.append(doneButton)
toolbar.setItems(buttons, animated: false)
toolbar.sizeToFit()


self.datePickerView.inputAccessoryView = toolbar
}

func doneButtonAction(){
self.view.endEditing(true)
}

答案 1 :(得分:1)

另一种方法是在datePickerValueChanged事件中首先关闭键盘,然后隐藏您的UIDatePicker

func datePickerValueChanged(sender:UIDatePicker) {
        let dateFormatter = DateFormatter()
        dateFormatter.dateStyle = DateFormatter.Style.short
        dateFormatter.timeStyle = DateFormatter.Style.none

        birthday.text = dateFormatter.string(from: sender.date)

       self.view.endEditing(true) // Hide keyboard

    }

答案 2 :(得分:1)

您也可以使用它。

 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.view.endEditing(true)
 }

使用此功能,您不需要任何完成按钮 它将通过在其外部点击来重新签名textField或textView 注意:它不会影响scrollView。

答案 3 :(得分:1)

添加以下两个操作,并在viewDidLoad函数中添加代码。

override func viewDidLoad() {
        super.viewDidLoad()
        datePickerView = UIDatePicker()
        datePickerView!.datePickerMode = .Date
        txtxt.inputView = datePickerView
        datePickerView!.addTarget(self, action: Selector("handleDatePicker:"), forControlEvents: UIControlEvents.ValueChanged)
        let toolBar = UIToolbar(frame: CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(self.view.frame.size.width), height: CGFloat(44)))
        toolBar.tintColor = UIColor.grayColor()
        let doneBtn = UIBarButtonItem(title: "Done", style: .Bordered, target: self, action:"ShowSelectedDate")

        let space = UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: nil, action: nil)

        toolBar.items = [space, doneBtn]
        txtxt.inputAccessoryView = toolBar
        // Do any additional setup after loading the view, typically from a nib.
    }
 func ShowSelectedDate(){
        self.view.endEditing(true)
    }

    func handleDatePicker(sender: UIDatePicker) {
        let dateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd"
        txtxt.text = dateFormatter.stringFromDate(sender.date)
    }