Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITextField with secure entry, always getting cleared before editing

I am having a weird issue in which my UITextField which holds a secure entry is always getting cleared when I try to edit it. I added 3 characters to the field, goes to another field and comes back, the cursor is in 4th character position, but when I try to add another character, the whole text in the field gets cleared by the new character. I have 'Clears when editing begins' unchecked in the nib. So what would be the issue? If I remove the secure entry property, everything is working fine, so, is this the property of Secure entry textfields? Is there any way to prevent this behaviour?

like image 791
Nithin Avatar asked Sep 05 '11 08:09

Nithin


2 Answers

Set,

textField.clearsOnBeginEditing = NO;

Note: This won't work if secureTextEntry = YES. It seems, by default, iOS clears the text of secure entry text fields before editing, no matter clearsOnBeginEditing is YES or NO.

like image 164
EmptyStack Avatar answered Oct 24 '22 16:10

EmptyStack


If you don't want the field to clear, even when secureTextEntry = YES, use:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

    NSString *updatedString = [textField.text stringByReplacingCharactersInRange:range withString:string];

    textField.text = updatedString;

    return NO;
}

I encountered a similar issue when adding show/hide password text functionality to a sign-up view.

like image 23
Eric Avatar answered Oct 24 '22 14:10

Eric