带有secureTextEntry的UITextField不遵守clearsOnBeginEditing

时间:2009-11-25 20:21:04

标签: iphone

如果我设置了clearsOnBeginEditing并且有一个安全的文本字段,如果你失去对该字段的关注并再次返回并开始输入它将始终清除文本字段。有没有人有办法解决这个问题?

4 个答案:

答案 0 :(得分:0)

我刚遇到同样的问题。我正在从textFieldDidBeginEditing:call。

中更改属性

将属性更改移动到textFieldShouldBeginEditing:call修复了问题。

答案 1 :(得分:0)

保存在属性中输入的文本。例如:

NSString *_password;

然后在代表中:

textFieldDidBeginEditing:(UITextField *)textField

指定textField.text = _password;

答案 2 :(得分:0)

我为此子类化了UITextField。我在屏幕上有一个单独的控件来屏蔽UITextField(用于打开和关闭安全输入),所以这对我来说是一个更适合我解决问题的地方:

@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

答案 3 :(得分:0)

这对我有用:返回FALSE以禁止textview更改并自行设置更改。

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

    [textField setText:newContent];
    return FALSE;
}