[Solved] How to set up UITextFieldDelegate for multiple text fields? [closed]


If your goal is to have all three text fields follow the same rule, set the delegate for all three. The shouldChangeCharactersIn only needs to check the “current” text field into which the user is currently typing.

A minor observation, but I also would avoid recreating the CharacterSet of allowed characters repeatedly. You can simply make that a property.

That reduces it down to something like:

private let allowedCharacters = CharacterSet(charactersIn: "ABCDEFGĞHIİJKLMNOÖPRSŞTUÜVYZ")

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    if (textField.text?.count ?? 0) - range.length + string.count > 1 {
        return false
    }

    return allowedCharacters.isSuperset(of: CharacterSet(charactersIn: string.localizedUppercase))
}

If you want them only entering uppercase characters, I would:

  • Change the keyboard capitalization to “All Characters”, so they are more likely to not enter lowercase letters:

    enter image description here

  • Optionally change the capitalization by adding an “editing changed” action for your text field to:

    @IBAction func editingChanged(_ textField: UITextField) {
        textField.text = textField.text?.localizedUppercase
    }
    

    You might have to experiment whether you want to use localizedUppercase or uppercased() or uppercased(with:).

    Note, this “uppercase the whole string” logic is a little sloppy. If you were allowing multi-character strings in your input, you really would want to capture where the cursor was and restore it. (Otherwise it could be an annoying UX where if the user is changing the first character of a multi-character string, the cursor would jump to the end.) E.g., a simple rendition might be:

    @IBAction func editingChanged(_ textField: UITextField) {
        let range = textField.selectedTextRange
        textField.text = textField.text?.localizedUppercase
        textField.selectedTextRange = range
    }
    

    But for your single character input, the simpler example, above, should be sufficient.

5

solved How to set up UITextFieldDelegate for multiple text fields? [closed]