UISwitch 在 on/off 后重置 TextField 值
UISwitch resets TextField value after on/off
我创造了
- (IBAction)passwordSwitchPressed:(id)sender {
if (self.passwordSwitch.isOn) {
self.passwordTextField.secureTextEntry = YES;
}
else {
self.passwordTextField.secureTextEntry = NO;
}
}
条件是如果开关打开,密码文本字段安全输入打开,否则关闭。一切正常,但如果我从打开更改为关闭,然后再返回打开,密码文本字段中的值就会消失。如何解决?
尽量参考输入,让我知道:)
- (IBAction)passwordSwitchPressed:(id)sender
{
NSString * passValue = self.passwordTextField.text;
self.passwordTextField.secureTextEntry = self.passwordSwitch.isOn;
self.passwordTextField.text = passValue;
}
将委托设置到文本字段后。试试下面的方法。希望有用。
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSString *oldString = [textField.text stringByReplacingCharactersInRange:range withString:string];
textField.text = oldString;
return NO;
}
我创造了
- (IBAction)passwordSwitchPressed:(id)sender {
if (self.passwordSwitch.isOn) {
self.passwordTextField.secureTextEntry = YES;
}
else {
self.passwordTextField.secureTextEntry = NO;
}
}
条件是如果开关打开,密码文本字段安全输入打开,否则关闭。一切正常,但如果我从打开更改为关闭,然后再返回打开,密码文本字段中的值就会消失。如何解决?
尽量参考输入,让我知道:)
- (IBAction)passwordSwitchPressed:(id)sender
{
NSString * passValue = self.passwordTextField.text;
self.passwordTextField.secureTextEntry = self.passwordSwitch.isOn;
self.passwordTextField.text = passValue;
}
将委托设置到文本字段后。试试下面的方法。希望有用。
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSString *oldString = [textField.text stringByReplacingCharactersInRange:range withString:string];
textField.text = oldString;
return NO;
}