iOS 6中的退格功能和iOS 5 for UITextfield,具有'secure'属性 [英] Backspace functionality in iOS 6 & iOS 5 for UITextfield with 'secure' attribute
问题描述
我有一个 UITextfield
用于输入密码,启用了 SECURE
属性。
I have a UITextfield
for entering password with SECURE
attribute enabled.
当用户在输入一定数量的字符后点击 UITextfield
之外,然后再次点击回到 UITextfield
进行编辑时,发生以下行为:
When the user taps outside the UITextfield
after inputting a certain number of characters, and then taps back again to the UITextfield
for editing, the following behavior is taking place:
iOS 5.1
- 当我尝试从 UITextfield
中删除一个字符(使用键盘中的 backspace
),删除最后一个字符。
iOS 5.1
-When I try to delete a character(using backspace
from keyboard) from the UITextfield
, the last character is deleted.
iOS 6.0
- 当我尝试从 UITextfield
中删除某个字符时,所有键入的字符都会被删除。
iOS 6.0
-When I try to delete a character from the UITextfield
, all typed characters are getting deleted.
我做错了什么,或者这是iOS 6中的错误?
Am I doing something wrong, or is this a bug in iOS 6?
推荐答案
这是iOS6下的预期行为,您不应该更改它。
This is the intended behaviour under iOS6 and you shouldn't probably change it.
如果出于任何原因你真的需要这个,你可以覆盖 UITextField
的委托方法 textField:shouldChangeCharactersInRange:replacementString:
并恢复旧行为:
If for whatever reason you really need this, you can override UITextField
's delegate method textField:shouldChangeCharactersInRange:replacementString:
and restore the old behaviour:
#import "ViewController.h"
@interface ViewController () <UITextFieldDelegate>
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.passwordField.delegate = self;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (range.location > 0 && range.length == 1 && string.length == 0)
{
// iOS is trying to delete the entire string
textField.text = [textField.text substringToIndex:textField.text.length - 1];
return NO;
}
return YES;
}
@end
这篇关于iOS 6中的退格功能和iOS 5 for UITextfield,具有'secure'属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!