Search code examples
iosobjective-ccocoa-touchuiswitch

UISwitch resets TextField value after on/off


I created

- (IBAction)passwordSwitchPressed:(id)sender {
    if (self.passwordSwitch.isOn) {
        self.passwordTextField.secureTextEntry = YES;

    }
    else {
        self.passwordTextField.secureTextEntry = NO;
    }
}


And condition is that if switch is On password text field secure entry is on, else off. Everything works perfect, but if I change from on to off, and then go back to on, value in password text field dissapear. How to solve this?


Solution

  • After setting the delegate to the textfield. Try the below way. Hope it will work.

    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
        NSString *oldString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    
        textField.text = oldString;
    
        return NO;
    }