简体   繁体   中英

UITextField not triggering action event upon UIDatePicker action event

I have a datePicker that updates my textField. Once the textField data changes is supposed to perform actions(such as verifying data). The problem is that after the datePicker updates the textField, the textField does not perform any actions, regardless of what action event I am using on the textField, such as .ValueChanged.I cannot figure out what the problem is. I noticed though, that if I am to type something in the textField, the action event works. Here's my code:

class CustomTextField: UITextField, UITextFieldDelegate {

    var customDelegate : CustomTextFieldDelegate?

    enum type {
        case standard
        case date
    }

    var textFieldType : type = .standard {
        didSet{
            switch self.textFieldType {
            case .standard:
                break
            case .date:
                let datePicker = UIDatePicker.MMMddyyyy
                self.inputView = datePicker
                datePicker.addTarget(self, action: #selector(refreshOnDatePicker(_:)), for: .valueChanged)
                self.addTarget(self, action: #selector(checkFieldInput), for: .applicationReserved)
                break
            }
        }
    }


    @objc private func checkFieldInput(sender: CustomTextField){
        guard let fieldInput = self.text else { return }
        switch sender.textFieldType {
            case .standard:
                break
            case .date:
                customDelegate?.isTextFieldVerified = !fieldInput.isEmpty
            break
        }
    }

    @objc private func refreshOnDatePicker(_ selector: UIDatePicker){
        self.text = selector.date.formattedMMMMddyyyy
    }
}


fileprivate extension UIDatePicker {
    class var MMMddyyyy : UIDatePicker {
        let currentDate: NSDate = NSDate()



    let calendar: NSCalendar = NSCalendar(calendarIdentifier: NSCalendar.Identifier.gregorian)!
    calendar.timeZone = NSTimeZone(name: "UTC")! as TimeZone

    let components: NSDateComponents = NSDateComponents()
    components.calendar = calendar as Calendar
    components.year = -100

    let minDate: NSDate = calendar.date(byAdding: components as DateComponents, to: currentDate as Date, options: NSCalendar.Options(rawValue: 0))! as NSDate

    let dp = UIDatePicker()
    dp.datePickerMode = .date
    dp.minimumDate = minDate as Date
    dp.maximumDate = currentDate as Date
    dp.backgroundColor = UIColor.white
    return dp
   }
}

fileprivate extension Date {
    /// date formatted type "MMMM dd, yyyy"
    var formattedMMMMddyyyy: String {
        let formatter = DateFormatter()
        formatter.dateFormat = "MMMM dd, yyyy"
        return  formatter.string(from: self)
    }
}

Try this

 @objc private func refreshOnDatePicker(_ selector: UIDatePicker){
    self.text = selector.date.formattedMMMMddyyyy
    self.checkFieldInput(sender:self)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM