iOS 6 UITextField Secure - 如何检测退格清除所有字符?

Posted

技术标签:

【中文标题】iOS 6 UITextField Secure - 如何检测退格清除所有字符?【英文标题】:iOS 6 UITextField Secure - How to detect backspace clearing all characters? 【发布时间】:2013-05-27 01:36:46 【问题描述】:

ios 6 中,如果您在安全文本字段中键入文本,然后更改为另一个文本字段,然后返回到安全文本字段并按退格键,则所有字符都会被删除。我对这种情况很好,但是,我正在尝试根据此安全文本字段中是否包含字符来启用/禁用按钮。我知道如何确定字段中的字符以及是否按下退格键,但我无法确定如何检测是否正在清除所有字符。

这是我用来获取字段的新文本的委托方法,但是,我似乎无法弄清楚如何获取新文本(假设新文本只是一个空白字符串)如果按下退格键会清除所有字符。

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

    //returns the "new text" of the field
    NSString * text = [textField.text stringByReplacingCharactersInRange:range withString:string];

非常感谢任何帮助。

谢谢!

【问题讨论】:

如果通过“replacementString:”参数传入的“string”是一个零长度的空字符串,而“range.length”大于零,我会说这是你的“一切都在清理”案子。 只是按退格键并删除一个字符会导致字符串参数为空且长度为零,因此不起作用。 range 返回好像它是一个后退空格,所以这也不起作用。我不确定你所说的 [self length] 是什么意思 【参考方案1】:

我使用这个解决方案。删除字符后,不需要局部变量并正确设置光标位置。

这是这个解决方案的混搭:

Backspace functionality in iOS 6 & iOS 5 for UITextfield with 'secure' attribute how to move cursor in UITextField after setting its value

 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
    
        if (range.location > 0 && range.length == 1 && string.length == 0)
        
            // Stores cursor position
            UITextPosition *beginning = textField.beginningOfDocument;
            UITextPosition *start = [textField positionFromPosition:beginning offset:range.location];
            NSInteger cursorOffset = [textField offsetFromPosition:beginning toPosition:start] + string.length;

            // Save the current text, in case iOS deletes the whole text
            NSString *text = textField.text;


            // Trigger deletion
            [textField deleteBackward];


            // iOS deleted the entire string
            if (textField.text.length != text.length - 1)
            
                textField.text = [text stringByReplacingCharactersInRange:range withString:string];

                // Update cursor position
                UITextPosition *newCursorPosition = [textField positionFromPosition:textField.beginningOfDocument offset:cursorOffset];
                UITextRange *newSelectedRange = [textField textRangeFromPosition:newCursorPosition toPosition:newCursorPosition];
                [textField setSelectedTextRange:newSelectedRange];
            

            return NO;
        

        return YES;
    

【讨论】:

【参考方案2】:

终于为任何想了解如何确定退格何时清除安全 UITextField 的所有字符的人解决了这个问题:

UITextField:

self.passwordTextField

私有属性(在 init 中初始化为 NO - 可能不需要):

self.passwordFirstCharacterAfterDidBeginEditing

UITextFieldDelegate 方法:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

    //if text is blank when first editing, then first delete is just a single space delete
    if([textField.text length] == 0 && self.passwordFirstCharacterAfterDidBeginEditing)
        self.passwordFirstCharacterAfterDidBeginEditing = NO;

    //if text is present when first editing, the first delete will result in clearing the entire password, even after typing text
    if([textField.text length] > 0 && self.passwordFirstCharacterAfterDidBeginEditing && [string length] == 0 && textField == self.passwordTextField)
    
        NSLog(@"Deleting all characters");
        self.passwordFirstCharacterAfterDidBeginEditing = NO;
    
    return YES;


-(void)textFieldDidBeginEditing:(UITextField *)textField

    if(textField == self.passwordTextField)
    
        self.passwordFirstCharacterAfterDidBeginEditing = YES;
    

我希望这对某人有所帮助,我也希望 Apple 创建一个委托方法,当安全文本字段被删除清除时调用该方法 - 这看起来很麻烦,但它确实有效。

【讨论】:

这是唯一对我有用的解决方案,其中嵌入了 UITableViewCells 中的 UITextFields。【参考方案3】:

无论您是删除还是输入文本,在 iOS6+ 上开始编辑时都会清除安全文本字段。如果文本字段从退格键中清除,则检查替换字符串的长度是否为 0 有效,但令人讨厌的是,如果在输入文本时清除文本,textField:shouldChangeCharactersInRange:replacementString: 的范围和替换字符串参数不正确。这是我的解决方案:

1) 注册textFieldTextDidChange 通知。

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(textFieldTextDidChange:)
                                             name:UITextFieldTextDidChangeNotification
                                           object:nil];

2) 如果文本字段是安全的并且可能已被清除,则重新调用 textField:shouldChangeCharactersInRange:replacementString: 委托方法。

- (void)textFieldTextDidChange:(NSNotification *)notification

    if (textField.secureTextEntry && textField.text.length <= 1) 
        [self.textField.delegate textField:self.textField
             shouldChangeCharactersInRange:NSMakeRange(0, textField.text.length)
                         replacementString:textField.text];
    

【讨论】:

【参考方案4】:

接受的解决方案不启用按钮。它实际上改变了安全文本字段上的退格行为。

这个 Swift 解决方案确实通过正确通知委托人将在 textField 中设置的实际字符串解决了这个问题,包括按下退格键时。然后,该代表可以做出决定。

    func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool 
    var shouldChange = true

    if let text = textField.text 
        let oldText = text.copy()
        var resultString = (text as NSString).stringByReplacingCharactersInRange(range, withString: string) as NSString
        let inputString = string as NSString
        if range.location > 0 && range.length == 1 && inputString.length == 0 //Backspace pressed
                textField.deleteBackward()
                shouldChange = false

                if let updatedText = textField.text as NSString? 
                    if updatedText.length != oldText.length - 1 
                        resultString = ""
                    
                
        
        self.delegate?.willChangeTextForCell(self, string: resultString as String)
    

    return shouldChange

【讨论】:

【参考方案5】:

我遇到了同样的问题,我想检测退格何时会清除所有字符,以便我可以启用禁用屏幕上的其他一些按钮。所以这就是我实现它的方式。

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField

    _checkForDelete = NO;
    if (textField.isSecureTextEntry && textField.text.length > 0) 
        _checkForDelete = YES;
    
    return YES;


-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

    if (_checkForDelete && string.length == 0) 
        _checkForDelete = NO;
        //Do whatever you want to do in this case, 
        //because all the text is going to be cleared.
    
    return YES

此解决方案不同,因为它允许您在安全字段中已经包含一些文本并且您尝试对其进行编辑时采取行动,而不是在上面接受的答案中,即使您在按下退格键时也会进入块已经在编辑一个字段,这不会清除整个文本,只会删除一个字符。使用我的方法,您可以选择在这种特殊情况下采取行动,但它也不会影响本机流程。

【讨论】:

【参考方案6】:

发布我的替代解决方案,因为我遇到了 OP 的确切问题,并且发现我不必在所选答案中详细说明任何光标位置更改。

下面是 UITextFieldDelegate 方法,我调用了一个自定义委托方法 didChangeValueForTextField,因为我要启用的按钮在这个类之外。

实现 UITextFieldDelegate 的类

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

if (range.location > 0 && range.length == 1 && string.length == 0)
    [textField deleteBackward];

    if ([textField.text isEmptyCheck])   // Check if textField is empty
        newValue = @"";
    

    shouldReturn = NO;


if(self.delegate && [self.delegate respondsToSelector:@selector(didChangeValueForField:withNewValue:)]) 
    [self.delegate didChangeValueForField:textField withNewValue:newValue];


return shouldReturn;

关键是检测安全字段单字符删除和触发字段清除的安全字段单字符删除之间的区别。在上述委托方法中检测到此 是必要的。

包含启用按钮的类

 - (void)didChangeValueForField:(UITextField *)textField withNewValue:(NSString *)value 
  // Update values used to validate/invalidate the button.
  // I updated a separate model here.

  // Enable button based on validity
  BOOL isEnabled = [self allFieldsValid];  //  Check all field values that contribute to the button being enabled.

  self.myButton.enabled = isEnabled;


【讨论】:

【参考方案7】:

swift3.0 - 可以。

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool

    if textField == passwordTextfield 

        if range.location > 0 && range.length == 1 && string.characters.count == 0 

            if let textString = textField.text 
                // iOS is trying to delete the entire string
                let index = textString.index(textString.endIndex, offsetBy: -1)
                textField.text = textString.substring(to: index)
                return false
            

        else if range.location == 0 
            return true
        else 
            if let textString = textField.text 
                textField.text = textString + string
                return false
            
        


    
    return true

【讨论】:

以上是关于iOS 6 UITextField Secure - 如何检测退格清除所有字符?的主要内容,如果未能解决你的问题,请参考以下文章

在 iOS 6 中,-[UITextField becomeFirstResponder] 在 -viewWillAppear 中不起作用:

添加 UITextField 作为 UITableViewCell 的子视图在 IOS 6 中工作正常,但在 IOS 7 中它不起作用?

如何使用UItextfield在xamarin IOS中只进行前进和后退文本框?

iOS 6.1 中的 *** 从 TextFieldOnEditingDidEnd 内部调用成为第一响应者时

IOS 13:UITextField rightView的间距问题

ios UITextField