swiftnsdateuidatepickeruiswipegesturerecognizer

Change Current Date To One Before / One After - Swift DatePicker


I have a date picker. I would like to change the date displayed to a date before or after using gesture control. Ie, when a person swipes left the date goes one day behind and if a person swipes right the date goes one day after. I am not sure how to implement this as I have tried the method below but i get an error stating 'Cannot convert value of type 'UITextField?' to expected argument type'Date'. If someone could help me out or point me in the right direction I would appreciate that a lot! Thanks

View Controller:

    @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
           }
    }

I tried this method but this is where I get the error:

Calendar.current.date(byAdding: .day, value: 1, to: userDate)!

Solution

  • You are trying to set a textField where the method expects a date here:

    Calendar.current.date(byAdding: .day, value: 1, to: userDate)!
    

    From your question it seems you want to either add or subtract a day from the current date displayed in the textField. Try the following code to do just that. This takes the date from userDate.text and either adds or subtracts a day based on the swipe direction the sets userDate.text to the new value.

    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)
            }
        }
    }