将当前日期更改为前一日期/后一日期 - Swift DatePicker
Change Current Date To One Before / One After - Swift DatePicker
我有一个日期选择器。我想将显示的日期更改为使用手势控制之前或之后的日期。即,当一个人向左滑动时,日期会晚一天,如果一个人向右滑动,日期会在一天后。我不确定如何实现它,因为我尝试了下面的方法,但我收到一条错误消息,指出“无法将类型 'UITextField?' 的值转换为预期的参数类型‘日期’。如果有人可以帮助我或指出正确的方向,我将不胜感激!谢谢
视图控制器:
@IBOutlet weak var userDate: UITextField!
private var datePicker: UIDatePicker?
override func viewDidLoad() {
super.viewDidLoad()
datePicker = UIDatePicker()
datePicker?.datePickerMode = .date
datePicker?.addTarget(self, action: #selector(Test.dateChanged(datePicker:)), for: .valueChanged)
userDate.inputView = datePicker
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEEE, MMM d, yyyy"
let todayDate = dateFormatter.string(from: Date.init())
self.userDate.text = todayDate
let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(self.handleGesture(gesture:)))
swipeLeft.direction = .left
self.view.addGestureRecognizer(swipeLeft)
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(self.handleGesture(gesture:)))
swipeRight.direction = .right
self.view.addGestureRecognizer(swipeRight)
}
@objc func handleGesture(gesture: UISwipeGestureRecognizer) -> Void {
if gesture.direction == UISwipeGestureRecognizer.Direction.right {
print("Swipe Right")
view.backgroundColor = .black
// WHERE I WOULD LIKE TO CHANGE THE DATE ONE DAY AFTER
}
else if gesture.direction == UISwipeGestureRecognizer.Direction.left {
print("Swipe Left")
view.backgroundColor = .yellow
// WHERE I WOULD LIKE TO CHANGE THE DATE ONE DAY BEFORE
}
}
我试过这个方法,但这是我收到错误的地方:
Calendar.current.date(byAdding: .day, value: 1, to: userDate)!
函数 date(byAdding:value:to:options:)
需要将日期类型作为参数传递。您正在传递 UITextField 而不是日期类型。
我想你想做的是
Calendar.current.date(byAdding: .day, value: 1, to: datePicker.date)!
您正在尝试设置一个文本字段,其中该方法需要一个日期:
Calendar.current.date(byAdding: .day, value: 1, to: userDate)!
根据您的问题,您似乎想要在文本字段中显示的当前日期中增加或减去一天。试试下面的代码来做到这一点。这从 userDate.text 中获取日期,并根据设置 userDate.text 的滑动方向增加或减去一天到新值。
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var userDate: UITextField!
private var datePicker: UIDatePicker?
private let dateFormatter = DateFormatter()
override func viewDidLoad() {
super.viewDidLoad()
datePicker = UIDatePicker()
datePicker?.datePickerMode = .date
// datePicker?.addTarget(self, action: #selector(Test.dateChanged(datePicker:)), for: .valueChanged)
userDate.inputView = datePicker
dateFormatter.dateFormat = "EEEE, MMM d, yyyy"
let todayDate = dateFormatter.string(from: Date.init())
self.userDate.text = todayDate
let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(self.handleGesture(gesture:)))
swipeLeft.direction = .left
self.view.addGestureRecognizer(swipeLeft)
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(self.handleGesture(gesture:)))
swipeRight.direction = .right
self.view.addGestureRecognizer(swipeRight)
}
@objc func handleGesture(gesture: UISwipeGestureRecognizer) -> Void {
if gesture.direction == UISwipeGestureRecognizer.Direction.right {
print("Swipe Right")
// WHERE I WOULD LIKE TO CHANGE THE DATE ONE DAY AFTER
let date = dateFormatter.date (from: userDate.text ?? dateFormatter.string(from: Date()))!
let plusDay = Calendar.current.date(byAdding: .day, value: 1, to: date)!
userDate.text = dateFormatter.string(from: plusDay)
}
else if gesture.direction == UISwipeGestureRecognizer.Direction.left {
print("Swipe Left")
// WHERE I WOULD LIKE TO CHANGE THE DATE ONE DAY BEFORE
let date = dateFormatter.date (from: userDate.text ?? dateFormatter.string(from: Date()))!
let minusDay = Calendar.current.date(byAdding: .day, value: -1, to: date)!
userDate.text = dateFormatter.string(from: minusDay)
}
}
}
我有一个日期选择器。我想将显示的日期更改为使用手势控制之前或之后的日期。即,当一个人向左滑动时,日期会晚一天,如果一个人向右滑动,日期会在一天后。我不确定如何实现它,因为我尝试了下面的方法,但我收到一条错误消息,指出“无法将类型 'UITextField?' 的值转换为预期的参数类型‘日期’。如果有人可以帮助我或指出正确的方向,我将不胜感激!谢谢
视图控制器:
@IBOutlet weak var userDate: UITextField!
private var datePicker: UIDatePicker?
override func viewDidLoad() {
super.viewDidLoad()
datePicker = UIDatePicker()
datePicker?.datePickerMode = .date
datePicker?.addTarget(self, action: #selector(Test.dateChanged(datePicker:)), for: .valueChanged)
userDate.inputView = datePicker
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEEE, MMM d, yyyy"
let todayDate = dateFormatter.string(from: Date.init())
self.userDate.text = todayDate
let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(self.handleGesture(gesture:)))
swipeLeft.direction = .left
self.view.addGestureRecognizer(swipeLeft)
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(self.handleGesture(gesture:)))
swipeRight.direction = .right
self.view.addGestureRecognizer(swipeRight)
}
@objc func handleGesture(gesture: UISwipeGestureRecognizer) -> Void {
if gesture.direction == UISwipeGestureRecognizer.Direction.right {
print("Swipe Right")
view.backgroundColor = .black
// WHERE I WOULD LIKE TO CHANGE THE DATE ONE DAY AFTER
}
else if gesture.direction == UISwipeGestureRecognizer.Direction.left {
print("Swipe Left")
view.backgroundColor = .yellow
// WHERE I WOULD LIKE TO CHANGE THE DATE ONE DAY BEFORE
}
}
我试过这个方法,但这是我收到错误的地方:
Calendar.current.date(byAdding: .day, value: 1, to: userDate)!
函数 date(byAdding:value:to:options:)
需要将日期类型作为参数传递。您正在传递 UITextField 而不是日期类型。
我想你想做的是
Calendar.current.date(byAdding: .day, value: 1, to: datePicker.date)!
您正在尝试设置一个文本字段,其中该方法需要一个日期:
Calendar.current.date(byAdding: .day, value: 1, to: userDate)!
根据您的问题,您似乎想要在文本字段中显示的当前日期中增加或减去一天。试试下面的代码来做到这一点。这从 userDate.text 中获取日期,并根据设置 userDate.text 的滑动方向增加或减去一天到新值。
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var userDate: UITextField!
private var datePicker: UIDatePicker?
private let dateFormatter = DateFormatter()
override func viewDidLoad() {
super.viewDidLoad()
datePicker = UIDatePicker()
datePicker?.datePickerMode = .date
// datePicker?.addTarget(self, action: #selector(Test.dateChanged(datePicker:)), for: .valueChanged)
userDate.inputView = datePicker
dateFormatter.dateFormat = "EEEE, MMM d, yyyy"
let todayDate = dateFormatter.string(from: Date.init())
self.userDate.text = todayDate
let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(self.handleGesture(gesture:)))
swipeLeft.direction = .left
self.view.addGestureRecognizer(swipeLeft)
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(self.handleGesture(gesture:)))
swipeRight.direction = .right
self.view.addGestureRecognizer(swipeRight)
}
@objc func handleGesture(gesture: UISwipeGestureRecognizer) -> Void {
if gesture.direction == UISwipeGestureRecognizer.Direction.right {
print("Swipe Right")
// WHERE I WOULD LIKE TO CHANGE THE DATE ONE DAY AFTER
let date = dateFormatter.date (from: userDate.text ?? dateFormatter.string(from: Date()))!
let plusDay = Calendar.current.date(byAdding: .day, value: 1, to: date)!
userDate.text = dateFormatter.string(from: plusDay)
}
else if gesture.direction == UISwipeGestureRecognizer.Direction.left {
print("Swipe Left")
// WHERE I WOULD LIKE TO CHANGE THE DATE ONE DAY BEFORE
let date = dateFormatter.date (from: userDate.text ?? dateFormatter.string(from: Date()))!
let minusDay = Calendar.current.date(byAdding: .day, value: -1, to: date)!
userDate.text = dateFormatter.string(from: minusDay)
}
}
}