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 :(