iphone - iOS 6 和 iOS 5 中具有 'secure' 属性的 UITextfield 的退格功能

标签 iphone ios ipad ios5 ios6

我有一个 UITextfield 用于在启用 SECURE 属性的情况下输入密码。

当用户在输入一定数量的字符后点击 UITextfield 外部,然后再次点击回到 UITextfield 进行编辑时,将发生以下行为:

iOS 5.1 - 当我尝试从 UITextfield 中删除一个字符(使用键盘上的 backspace)时,最后一个字符被删除。

iOS 6.0 - 当我尝试从 UITextfield 中删除一个字符时,所有键入的字符都会被删除。

我是不是做错了什么,或者这是 iOS 6 中的错误?

最佳答案

这是 iOS6 下的预期行为,您不应该更改它。

如果出于某种原因你真的需要这个,你可以覆盖 UITextField 的委托(delegate)方法 textField:shouldChangeCharactersInRange:replacementString: 并恢复旧的行为:

#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

关于iphone - iOS 6 和 iOS 5 中具有 'secure' 属性的 UITextfield 的退格功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14400724/

相关文章:

iphone - 在 iPhone/iPad 通用应用程序中管理图像

ios - Firebase & JSQMessagesViewController 在 Ios Swift 2 中的一对一聊天

ios - "Missing argument for parameter ' 样式' in call"创建系统UIBarButtonItem 错误

Javascript iPad、iPhone 和 Android - 如何获取页面的全宽和全高?

objective-c - 在 UIWebView iPad App 中解析 XML 并生成 UI

iphone - 如何在 UIView 上通过触摸移动绘制任意路径

iphone - 在 UITableViewCell 中编辑 TextLabel

iphone - 在旧 iPhone/iPod Touch 上测试应用程序是否有意义

iphone - 使用 Storyboard 将 UINavigationController 插入 UIViewController

ios - 在 Swift for iOS 中使用 AVFoundation 录制视频