gpt4 book ai didi

ios - 如何在 swift 的第二个日期选择器中显示第一个日期选择器的选定日期

转载 作者:行者123 更新时间:2023-11-28 14:27:11 25 4
gpt4 key购买 nike

我在我的代码中使用了两个 datePickers,当我们点击第二个 datePicker 时,我想在第二个中显示第一个 datePicker 的选定日期>。为此,我使用了 DatePickerDialog 的 pod。我已经尝试了所有方法,但我无法将第一个选定日期设置为第二个 datePicker 的开始日期。为此,我正在使用 pod DatePickerDialog。请根据此告诉我解决方案。我正在使用这个 pods 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)
}

最佳答案

在使用之前,您应该始终阅读 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) {

关于ios - 如何在 swift 的第二个日期选择器中显示第一个日期选择器的选定日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51472011/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com