我想在一个文本字段中可以显示键盘和日期选择器

时间:2019-08-07 10:00:59

标签: ios swift datepicker

我想在用户单击右侧的button时显示键盘,并在用户单击datePicker时显示textField

@IBAction func doEditBirthDate(_ sender: UITextField) {
    if (txfBirthDate.isEditing){
        btnPhotoIdCard.isHidden = false
        lblPhotoIdCard.isHidden = false
        let mDatePicker: UIDatePicker = UIDatePicker()

        mDatePicker.datePickerMode = .date

        sender.inputView = mDatePicker

        mDatePicker.addTarget(self, action: #selector(birthDateChanged(sender:)), for: .valueChanged)
    }else {
        btnPhotoIdCard.isHidden = true
        lblPhotoIdCard.isHidden = true
    }
}

@objc func birthDateChanged(sender: UIDatePicker) {
    let mDateFormatter = DateFormatter()

    mDateFormatter.locale = Locale(identifier: "en_US_POSIX")
    mDateFormatter.dateFormat = "dd MMMM yyyy"

    self.txfBirthDate.text = mDateFormatter.string(from: sender.date)
}

那只是显示日期选择器,但我想在右侧文本字段上添加按钮,如果用户单击它,可以显示键盘并将其文本输入文本字段

对不起我的英语

2 个答案:

答案 0 :(得分:0)

您必须尝试

class yourViewController: UIViewController,UITextFieldDelegate 
{
   var screenHeight = UIScreen.main.bound.height
   var screenWidth = UIScreen.main.bound.width
   var dateFormatter = DateFormatter()
   var doneButton = UIBarButtonItem()
   var cancelButton = UIBarButtonItem()
   var timeValueLabel = UIBarButtonItem()
   var datePicker : UIDatePicker!
   var toolBarDtPicker = UIToolbar()


 //MARK:- Life Cycle
   override func viewDidLoad() {
   super.viewDidLoad()

   yourTxtField.delegate = self
  }

 //TextField Editing Begin Function
 func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool
 {
    self.hideKeyboard()
    textField.inputView = nil
    textfieldEding(sender: textField)
    return false
 }

 //To Hide Keyboard
 @objc func hideKeyboard()
 {
   UIApplication.shared.sendAction(#selector(UIApplication.resignFirstResponder), to: nil, from: nil, for: nil)
 }


 //DatePicker Function
func datePickerView()
{

  if ((self.datePicker != nil) && (self.datePicker.superview != nil))
  {
     datePicker.removeFromSuperview()
  }

  self.datePicker = UIDatePicker(frame: CGRect(x: 0, y: 
  UIScreen.main.bounds.height-200, width: UIScreen.main.bounds.width, height: 200))
   self.datePicker.backgroundColor = UIColor.white

   self.datePicker.addTarget(self, action: #selector(dateHandler), for: .valueChanged)
   self.view.addSubview(datePicker)

   toolBarDtPicker = UIToolbar(frame: CGRect(x: 0, y: screenHeight-230, width: screenWidth, height: 30))
   toolBarDtPicker.barStyle = .default
   toolBarDtPicker.isTranslucent = false
   toolBarDtPicker.barTintColor = UIColor.blue
   toolBarDtPicker.sizeToFit()

   // Adding Button ToolBar
   doneButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(doneClick))
   let spaceButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
   timeValueLabel = UIBarButtonItem(title: " ", style: .plain, target: self, action: nil)
   let spaceButton2 = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
   cancelButton = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(cancelClick))
   doneButton.tintColor = UIColor.white
   timeValueLabel.tintColor = UIColor.white
   cancelButton.tintColor = UIColor.white
   toolBarDtPicker.setItems([cancelButton, spaceButton, timeValueLabel, spaceButton2, doneButton], animated: true)
   toolBarDtPicker.isUserInteractionEnabled = true

   self.view.addSubview(toolBarDtPicker)
   self.toolBarDtPicker.isHidden = false

   datePicker.datePickerMode = .date
   dateFormatter.dateStyle = .medium
   dateFormatter.dateFormat = "EEEE dd MMM yyyy"

 }


 @objc func textfieldEding(sender: UITextField)
 {
     datePickerView()
 }

 @IBAction func showKeyBoard(_ sender: UIButton!)
 {        
     self.yourTxtField.becomeFirstResponder()
 }

 //MARK: DatePicker

 @objc func dateHandler(sender: UIDatePicker)
 {
     dateFormatter.dateFormat = "EEEE dd MMM yyyy"
     timeValueLabel.title = dateFormatter.string(from: sender.date)
 }

 @objc func doneClick() //DatePIcker Done Button
 {
    //Done Button Code
 }

 //MARK: Cancel Btn

 @objc func cancelClick() //DatePIcker Cancel Button
 {
    if ((self.datePicker != nil) && (self.datePicker.superview != nil))
    {
      datePicker.removeFromSuperview()
    }
    toolBarDtPicker.removeFromSuperview()
  }

}

答案 1 :(得分:-1)

使用此代码:我尝试过此方法。

ArrayList<ArrayList<Integer>> mainArrayList = 
new ArrayList<ArrayList<Integer>>();