对同一个UITextField使用两个输入视图

时间:2017-02-14 07:48:47

标签: ios iphone swift keyboard uitextfield

我有一个UITextField可以使用数据选择器和数字键盘输入日期,当用户直接点击UITextField时,数字键盘应该打开,但是当用户点击UIButton时,DatePicker应该打开。

我的代码:

userInfo.addTarget(self, action: #selector(textFieldEditing), for: .editingDidBegin)
func textFieldEditing(sender: UITextField) {
         sender.inputView = nil
         sender.inputAccessoryView = nil
         sender.reloadInputViews()
}
func pickUpDate(){

        // DatePicker
        datePicker = UIDatePicker()
        datePicker.backgroundColor = UIColor.white
        datePicker.setValue(Constants.Colors.AppGreen, forKey: "textColor")
        datePicker.datePickerMode = UIDatePickerMode.date
        datePicker.addTarget(self, action: #selector(datePickerValueChanged), for: UIControlEvents.valueChanged)

        let gregorian: NSCalendar = NSCalendar(calendarIdentifier: NSCalendar.Identifier.gregorian)!
        let currentDate: Date = Date()
        let components: NSDateComponents = NSDateComponents()


        if txtFDOB.textFieldType == UserBasicDetailCellType.UserBasicDetailCellDOBType {
            components.year = -18
            let minDate: Date = gregorian.date(byAdding: components as DateComponents, to: currentDate as Date, options: NSCalendar.Options(rawValue: 0))!

            components.year = -150
            let maxDate: Date = gregorian.date(byAdding: components as DateComponents, to: currentDate as Date, options: NSCalendar.Options(rawValue: 0))!
            datePicker.maximumDate = minDate
            datePicker.minimumDate = maxDate

            datePicker.date = minDate
        }
        if txtFDOB.textFieldType == UserBasicDetailCellType.UserBasicDetailCellNomineeDOBType {
            components.year = -18
            let minDate: Date = gregorian.date(byAdding: components as DateComponents, to: currentDate as Date, options: NSCalendar.Options(rawValue: 0))!
            datePicker.minimumDate = minDate
            datePicker.maximumDate = currentDate
        }

        // ToolBar
        let toolBar = UIToolbar()
        toolBar.barStyle = .default
        toolBar.isTranslucent = true
        toolBar.tintColor = Constants.Colors.DarkGrey
        toolBar.sizeToFit()

        // Adding Button ToolBar
        let doneButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(KycFormViewController.doneClick))
        doneButton.setTitleTextAttributes([NSFontAttributeName : UIFont.systemFont(ofSize: 18.0),NSForegroundColorAttributeName : Constants.Colors.AppGreen,NSBackgroundColorAttributeName:Constants.Colors.LightGrey],for: UIControlState.normal)

        let spaceButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
        let cancelButton = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(KycFormViewController.cancelClick))
        cancelButton.setTitleTextAttributes([NSFontAttributeName : UIFont.systemFont(ofSize: 18.0),NSForegroundColorAttributeName : Constants.Colors.AppGreen,NSBackgroundColorAttributeName:UIColor.black],
                                            for: UIControlState.normal)
        toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
        toolBar.isUserInteractionEnabled = true


        txtFDOB.resignFirstResponder()
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
            self.txtFDOB.inputAccessoryView = toolBar
            self.txtFDOB.inputView = self.datePicker
            self.txtFDOB.becomeFirstResponder()
        }        
    }

现在当我resignFirstResponder()并再次生成FirstResponder时,调用textFieldEditing并将InputViews设置为nil。

实现相同的任何其他方式。

提前致谢

1 个答案:

答案 0 :(得分:1)

喜欢

<强>步骤-1

在methof func pickUpDate()

中删除以下代码
txtFDOB.resignFirstResponder()
    DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
        self.txtFDOB.inputAccessoryView = toolBar
        self.txtFDOB.inputView = self.datePicker
        self.txtFDOB.becomeFirstResponder()
    }     

<强>步骤-2

call/Move your `func pickUpDate()` to `viewdidload` 

<强>步骤-3

您的文字字段func textFieldEditing(sender: UITextField)是正确的,就像您在按钮中一样,就像这样

 func datePickerClicked(sender: UIButton!) { 
txtFDOB.resignFirstResponder() 
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { 
self.txtFDOB.inputAccessoryView = toolBar 
self.txtFDOB.inputView = self.datePicker 
self.txtFDOB.becomeFirstResponder() 
} 
}