1

I'm setting programmatically text in textfield, the text is a birthdate, but the delegate not works, if I use a native keyboard the delegate works, but if I set a text programmatically on a textfield, the delegates not works

I'm using swift 4.2

import UIKit

class userBirthdateViewController: UIViewController, UITextFieldDelegate, NumberCalculable {

    @IBOutlet weak var messageBirthdate: UILabel!
    @IBOutlet weak var inputBirthdate: UITextField!
    @IBOutlet weak var keyboardView: keyboardView!


    override func viewDidLoad() {
        super.viewDidLoad()
        messageBirthdate.text = "Debes tener 18 años cumplidos para\npoder abrir una cuenta Flink"
        inputBirthdate.attributedPlaceholder = NSAttributedString(string: "DD-MM-AAAA", attributes: [NSAttributedString.Key.foregroundColor: UIColor.gray])
        self.inputBirthdate.inputView = UIView()
        self.inputBirthdate.becomeFirstResponder()
        keyboardView.delegate = self
        inputBirthdate.delegate = self
        // Do any additional setup after loading the view.
    }

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        if textField == inputBirthdate {

            // check the chars length dd -->2 at the same time calculate the dd-MM --> 5
            if (inputBirthdate?.text?.count == 2) || (inputBirthdate?.text?.count == 5) {
                //Handle backspace being pressed
                if !(string == "") {
                    // append the text
                    inputBirthdate?.text = (inputBirthdate?.text)! + "-"
                }
            }
            // check the condition not exceed 9 chars
            return !(textField.text!.count > 9 && (string.count ) > range.length)
        }
        else {
            return true
        }
    }

    @IBAction func probe(_ sender: Any) {

    }

    func addNumber(_ number: Int) {
        if number != -1
        {
            inputBirthdate.insertText("\(number)")
        }
        else
        {
            if !inputBirthdate.text!.isEmpty
            {
                inputBirthdate.text?.removeLast()
            }
        }
    }
}

I expect that the textfield responds like a native keyboard, the only difference is that I set programmatically the text on textfield.

I use this line self.inputBirthdate.inputView = UIView() because I don't want that the native keyboard appears, but only the cursor

how I can resolve this?

I tried with set insertText but not works :(

Andres Gomez
  • 458
  • 3
  • 12

1 Answers1

1

Delegate methods responds to user input only. you need to manually call the delegate methods after setting the text.

As I see in your question you don't want to display the default keyboard for inputting the date of birth. I would suggest you to use a UIDatePicker in place of Keyboard, this way you don't need to do the validations yourself. Moreover you can also specify minimum and maximum age limit with the help of UIDatePicker. On how to implement this. https://stackoverflow.com/a/32153107/2299040 is a great post

Sahil Manchanda
  • 9,812
  • 4
  • 39
  • 89