I've used two secure textfields(new password & confirm password), When I tried to clear a single character in a textField using backspace. It clears the whole text. How to fix this issue
Asked
Active
Viewed 770 times
1 Answers
1
You need to detect backspace
tapped and then remove last element from string with using textField
delegate
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
let char = string.cString(using: String.Encoding.utf8)!
let isBackSpace = strcmp(char, "\\b")
if (isBackSpace == -92) {
print("Backspace pressed")
}
return true
}
Dont forget to add UITextFieldDelegate to your ViewController

Omer Tekbiyik
- 4,255
- 1
- 15
- 27
-
let isBackSpace = strcmp(char, "\b") -> Invalid escape sequence in literal – Vikraman R Jun 08 '20 at 11:12
-
try "\\b", this should get rid of that error – Scriptable Jun 08 '20 at 11:15
-
Can you please explain how it works? – Vikraman R Jun 08 '20 at 11:26
-
I can but my english might not be enough to explain it :). This is the good explanation https://stackoverflow.com/a/51577826/9110909 @VikramanR – Omer Tekbiyik Jun 08 '20 at 11:33