Mobile Developer
Mobile Developer

Reputation: 5760

simple question about UITextField and not working secureTextEntry

I have a password field with text "Password" and when user clicks, it gets cleared. Moreover I want to set the type of this textfield as secure but this doesn´t work.

- (void)textFieldDidBeginEditing:(UITextField *)textField{

  if ([textField.text isEqualToString:@"Password"] || [textField.text isEqualToString:@"Email"])
  {
  textField.text = @"";
      textField.secureTextEntry = YES;
  }
}

Upvotes: 1

Views: 2682

Answers (6)

TS.xy
TS.xy

Reputation: 1234

If your keyboard is present, secureTextEntry won't work. Because the UITextInputTraits Protocol doesn't change the textfield if the keyboard shows. You can dismiss the keyboard, and it will work

[myTextField resignFirstResponder]

Upvotes: 0

Rahul Vyas
Rahul Vyas

Reputation: 28720

set the property in interface builder or when you initialize textfield remove the line from

-(void)textFieldDidBeginEditing:(UITextField *)textField {
}

I think you should set placeholder text as email or password and pre select the secure text entry from interface builder.That's it...

Upvotes: 1

David Robles
David Robles

Reputation: 373

I've just had the same problem. I was changing the property from within the textFieldDidBeginEditing: call.

Moving the property change to the textFieldShouldBeginEditing: call fixed the problem. I believe the trick is to change it while the text field isn't the becomeFirstResponder.

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    if ([textField isEqual:_passwordField]) {
        textField.secureTextEntry = YES;
    }

    return YES;
}

Upvotes: 6

Matt S.
Matt S.

Reputation: 1892

I realize this is a little old, but in iOS 6 the UITextField "text" is now by default "Attributed" in Interface Builder. Switching this to be "Plain", which is how it was in iOS 5, fixes this problem.

Upvotes: 1

cloosen
cloosen

Reputation: 993

- (void)textFieldDidBeginEditing:(UITextField *)textField{

  if ([textField.text isEqualToString:@"Password"] || [textField.text isEqualToString:@"Email"])
  {
  textField.text = @"";
    [textField resignFirstResponder];
    textField.secureTextEntry = YES;
    [textField becomeFirstResponder];
  }
}

it can solve your problem , but it has a big bug. the shit UITextField .

Upvotes: 2

Abdurrashid Khatri
Abdurrashid Khatri

Reputation: 338

Simply put your text in a placeholder and make your password textfield secure. Your problem will be solved :).

Upvotes: 2

Related Questions