UITextField with secureTextEntry does not respect clearsOnBeginEditing

If I set cleararsOnBeginEditing and you have a protected textbox and if you lose focus on the field and come back again and start typing, it will always clear the textbox. Does anyone have a way to get around this?

+1


source to share


4 answers


I had the same problem. I changed the property from the text textFieldDidBeginEditing: call.



Moving property change to textFieldShouldBeginEditing: call fixed the issue.

-1


source


save the text entered into the property. eg:

NSString *_password;

      

and then in the delegate:



textFieldDidBeginEditing:(UITextField *)textField

      

to appoint textField.text = _password;

0


source


I subclassed UITextField for this. I have a separate on-screen control for masking the UITextField (to turn protected write on and off), so this was a more appropriate place to tackle the problem:

@interface ZTTextField : UITextField {
    BOOL _keyboardJustChanged;
}
@property (nonatomic) BOOL keyboardJustChanged;
@end

@implementation ZTTextField
@synthesize keyboardJustChanged = _keyboardJustChanged;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        _keyboardJustChanged = NO;
    }
    return self;
}

- (void)insertText:(NSString *)text {
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000
    if (self.keyboardJustChanged == YES) {
        BOOL isIOS7 = NO;
        if ([[UIApplication sharedApplication] respondsToSelector:@selector(backgroundRefreshStatus)]) {
            isIOS7 = YES;
        }
        NSString *currentText = [self text];
        // only mess with editing in iOS 7 when the field is masked, wherein our problem lies
        if (isIOS7 == YES && self.secureTextEntry == YES && currentText != nil && [currentText length] > 0) {
            NSString *newText = [currentText stringByAppendingString: text];
            [super insertText: newText];
        } else {
            [super insertText:text];
        }
        // now that we've handled it, set back to NO
        self.keyboardJustChanged = NO;
    } else {
        [super insertText:text];
    }
#else
    [super insertText:text];
#endif
}

- (void)setKeyboardType:(UIKeyboardType)keyboardType {
    [super setKeyboardType:keyboardType];
    [self setKeyboardJustChanged:YES];
}

@end

      

0


source


This works for me: return FALSE to disallow changing the text and set the change yourself.

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

    [textField setText:newContent];
    return FALSE;
}

      

0


source







All Articles