1
votes

I have a UITextField that is a password field declared by using

passwordField.secureTextEntry = YES;

My issue is that when I type half my password, click on a different component, and come back to the passwordField and start typing again, iOS clears the existing text.

Is there a way to prevent this from happening or turn it off?

-Henry

3

3 Answers

4
votes

Simplest way to do this is to implement delegate for UITextField with a code similar to this:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)replacementString {
  if (textField.text) {
      textField.text = [textField.text stringByReplacingCharactersInRange:range withString:replacementString];
  } else {
      textField.text = replacementString;
  }

  return NO;
}
2
votes

By default, no, you can't do it for secure text fields. You can work around it though.

Implement the delegate method textField:shouldChangeCharactersInRange:replacementString: and use the values passed to decide what to do.

i.e. if the range is the entire string and the replacement is an empty string, don't allow the change. The new text might be provided as the newly typed character, you'll need to check what parameters the method actually receives.

0
votes

Well I have the same situation when I was changing keyboard type to make my password combo of 4(digits)+1(alpha), to stop clearing secureTextEntry,

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
textField.keyboardType = UIKeyboardTypePhonePad;
[textField reloadInputViews];

return YES;
}

It reloads UITextField but not its text, so without clearing text it changed the keyboard from alpha to numeric vice versa. You can use it for your own functionality.