我在代码中使用了两个datePickers
,当我们单击第二个datePicker
时,我想显示第二个中的第一个datePicker
的选定日期。为此,我使用DatePickerDialog
的pod。我尝试了所有方法,但我无从得知如何将第一个选定日期设置为第二个datePicker
的开始日期。我为此使用pod DatePickerDialog
。请根据这个告诉我解决方案。
我正在使用这个吊舱
https://github.com/squimer/DatePickerDialog-iOS-Swift
请参阅我的代码:
if textField == self.leaveField {
ShowDropDown()
textField.endEditing(true)
}else if textField == self.fromDateField {
DatePickerDialog().show("Select Date", doneButtonTitle: "Done", cancelButtonTitle: "Cancel", datePickerMode: .date) {
(date) -> Void in
if let dt = date {
//UserDefaults.standard.set(dt, forKey: "startDate")
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
self.fromDateField.text = formatter.string(from: dt)
}
textField.endEditing(true)
}
}else if textField == self.toDateField {
//let updatedDate = UserDefaults.standard.value(forKey: "startDate")
DatePickerDialog().show("Select Date", doneButtonTitle: "Done", cancelButtonTitle: "Cancel", datePickerMode: .date) {
(date) -> Void in
if let dt = date {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
self.toDateField.text = formatter.string(from: dt)
let start = self.fromDateField.text
let end = self.toDateField.text
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let startDate:NSDate = dateFormatter.date(from: start!) as! NSDate
let endDate:NSDate = dateFormatter.date(from: end!) as! NSDate
let components = Date.daysBetween(start: startDate as Date, end: endDate as Date)
//let components = cal.components(unit, fromDate: startDate, toDate: endDate, options: nil)
//let components = cal.components(unit, fromDate: startDate, toDate: endDate, options: nil)
self.totalDaysLbl.text = "Total Days " + "\(components)"
let char: Character = "-"
let string = self.totalDaysLbl.text
let result = string?.range(of: "\(char)")
if let result1 = result{
let otherAlert = UIAlertController(title: "Sorry!", message: "You cannot set the date in negative.", preferredStyle: UIAlertControllerStyle.alert)
let dismiss = UIAlertAction(title: "OK", style: UIAlertActionStyle.cancel, handler: nil)
otherAlert.addAction(dismiss)
self.present(otherAlert, animated: true, completion: nil)
self.totalDaysLbl.text = ""
}
}
}
textField.endEditing(true)
}
答案 0 :(得分:1)
在使用前,应始终阅读API文档。您可以在show
方法中传递一些参数来实现此目的。您可以先将第一个textField
字符串转换为date
,如下所示,
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
let selectedDate = formatter.date(from: self.fromDateField.text!)
然后使用下面的方法将此date
传递为defaultDate
,
open func show(_ title: String,
doneButtonTitle: String = "Done",
cancelButtonTitle: String = "Cancel",
defaultDate: Date = Date(),
minimumDate: Date? = nil,
maximumDate: Date? = nil,
datePickerMode: UIDatePickerMode = .dateAndTime,
callback: @escaping DatePickerCallback) {
答案 1 :(得分:0)
从第一个日期选择器中选择日期后,将其存储在变量中。
然后使用-
SeconddatePicker.date = //selectedDate
它将为您的第二个日期选择器设置tge缺省日期。