如何快速将一个 DatePicker 添加到三个文本字段中

Posted

技术标签:

【中文标题】如何快速将一个 DatePicker 添加到三个文本字段中【英文标题】:How to add one DatePicker to three textfields in swift 【发布时间】:2021-07-03 10:26:11 【问题描述】:

为此,我需要将 DatePicker 添加到三个文本字段中

我已经创建了一个如下所示的日期选择器

func addInputViewDatePicker(target: Any, selector: Selector) 

let screenWidth = UIScreen.main.bounds.width

//Add DatePicker as inputView
let datePicker = UIDatePicker(frame: CGRect(x: 0, y: 0, width: screenWidth, height: 216))
datePicker.datePickerMode = .date
self.inputView = datePicker

//Add Tool Bar as input AccessoryView
let toolBar = UIToolbar(frame: CGRect(x: 0, y: 0, width: screenWidth, height: 44))
let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
let cancelBarButton = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(cancelPressed))
let doneBarButton = UIBarButtonItem(title: "Done", style: .plain, target: target, action: selector)
toolBar.setItems([cancelBarButton, flexibleSpace, doneBarButton], animated: false)

self.inputAccessoryView = toolBar


@objc func cancelPressed() 
 self.resignFirstResponder()

我将 datepicker 添加到 doneButtonPressed 中的三个文本字段中,如下所示

    @objc func doneButtonPressed() 
    
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd"   
 
    if let  datePicker = self.fromDateTextfield.inputView as? UIDatePicker 
    
        self.fromDateTextfield.text = dateFormatter.string(from: datePicker.date)

    
    if let  datePicker = self.toDateTextfield.inputView as? UIDatePicker 
      
        self.toDateTextfield.text = dateFormatter.string(from: datePicker.date)
    
    if let  datePicker = self.lastbidDateTextfield.inputView as? UIDatePicker 
       
        self.lastbidDateTextfield.text = dateFormatter.string(from: datePicker.date)
    
    
    self.lastbidDateTextfield.resignFirstResponder()
    self.toDateTextfield.resignFirstResponder()
    self.fromDateTextfield.resignFirstResponder()
 

在 viewdidload 中:

override func viewDidLoad() 
    super.viewDidLoad()        
    fromDateTextfield.addInputViewDatePicker(target: self, selector: #selector(doneButtonPressed))
    toDateTextfield.addInputViewDatePicker(target: self, selector: #selector(doneButtonPressed))
    lastbidDateTextfield.addInputViewDatePicker(target: self, selector: #selector(doneButtonPressed))

 

如果我在fromDateTextfield 中添加日期,则使用上面的代码在所有三个文本字段中添加相同的日期.. 无需转到剩余的两个文本字段

我想在三个文本字段中添加不同的日期。如何使用上面的代码做到这一点。

如果我上面的代码有误,请帮助我为三个三个文本字段提供新的正确工作代码

【问题讨论】:

【参考方案1】:

为从 0 到 2 的每个 UIBarButtonItem 设置一个标签

func addInputViewDatePicker(target: Any, selector: Selector,tag:Int)
  .....
  doneBarButton.tag = tag
 
@objc func doneButtonPressed(_ sender: UIBarButtonItem) 
  let dateFormatter = DateFormatter()
  dateFormatter.dateFormat = "yyyy-MM-dd"   
  let arr = [fromDateTextfield,toDateTextfield,lastbidDateTextfield]
  let current = arr[sender.tag]
  if let datePicker = current.inputView as? UIDatePicker 
    current.text = dateFormatter.string(from: datePicker.date)
    current.resignFirstResponder()
  

【讨论】:

以上是关于如何快速将一个 DatePicker 添加到三个文本字段中的主要内容,如果未能解决你的问题,请参考以下文章

jQuery datepicker 在 IE9 中不工作并添加了动态文本区域

无法快速将特定日期和时间添加到 UserNotifications

如何将 DatePicker 添加到我的 MVC3 C# 站点

将样式/类添加到 react-datepicker

将 Telerik Datepicker 添加到动态添加的表单字段

将日历 jquery 图像添加到 datepicker