1

I'm trying to make a show/hide password textfield toggle in Objective-C, but I'm having an issue with secure text entry.

I've seen some answers to this question, (this and this), but I can't get myself around the problem.

So, I have a button that interacts with the text field and toggles secure text entry.

- (IBAction)showHidePassword:(id)sender {
    UIButton *btn = sender;
    if (self.passTextField.secureTextEntry == YES) {
        self.passTextField.secureTextEntry = NO;
        [self.toggleBTN setImage:[UIImage systemImageNamed:@"eye.fill"] forState:UIControlStateNormal];
    } else {
        self.passTextField.secureTextEntry = YES;
        [self.toggleBTN setImage:[UIImage systemImageNamed:@"eye"] forState:UIControlStateNormal];
    }
}

The problem is that when the user toggles from show to hide again, the text disappears and has to start again. This seems to be the default behavior in iOS.

I don't know if there is a way around this using button actions and not functions, or maybe I could hide the text with another property instead of secure text.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jas Har
  • 31
  • 5
  • Read the contents, change `secureTextEntry`, and then set contents again. – Sulthan Dec 03 '21 at 10:18
  • Hi @Sulthan already did that, apparently what secureTextEntry does is not only erase the text but it kinda resets the text input or something, so no matter what I set for the textfield it will be blanked – Jas Har Dec 03 '21 at 12:16

0 Answers0