2013-05-27 26 views
13

在iOS 6中,如果您將文本輸入到安全文本字段中,請更改爲其他文本字段,然後返回到安全文本字段並點擊退格鍵,將刪除所有字符。我很好,但是,我試圖啓用/禁用基於該安全文本字段是否有字符的按鈕。我知道如何確定字段中的字符,並且如果退格被擊中,但我無法確定如何檢測是否清除所有字符。iOS 6 UITextField Secure - 如何檢測退格清除所有字符?

這是我用來獲取字段的新文本的委託方法,但是,我似乎無法弄清楚如何獲取新文本(假設新文本只是一個空白字符串)如果退格命令清除所有字符。

- (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]; 
} 

任何幫助,非常感謝。

謝謝!

+0

如果通過參數「replacementString:」傳入的「字符串」是一個長度爲零的空字符串,而「range.length」大於零,那麼我會說你的「一切都是被清除「的情況。 –

+0

只是打退格和刪除一個字符將導致字符串參數爲空,長度爲零,所以這將無法正常工作。 – crizzwald

+0

範圍返回,就好像它是單個後退空間一樣,因此也不起作用。我不確定你的意思是[self length] – crizzwald

回答

14

我使用這個解決方案。它不需要局部變量,並在刪除char之後正確設置光標位置。

正是這種解決方案的混搭:


- (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; 
    } 
+0

很優雅。我喜歡。 – crizzwald

+0

你是一個天才兄弟! – Suran

5

終於想出任何人都希望看到如何確定何時退格是要清除安全的UITextField的所有字符:

的UITextField:

self.passwordTextField 

私人財產(初始化爲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創建一個委託方法,當一個安全的文本字段被刪除時被調用 - 這看起來很麻煩,但它的工作原理。

+0

這是唯一的解決方案,用UITableViewCells中嵌入的UITextFields工作。 – Keller

3

安全文本字段明確在開始編輯在iOS6的+,無論您要刪除或進入的文本。如果文本字段從退格中清除,檢查替換字符串的長度是否有效,但令人煩惱的是,如果文本在輸入文本時被清除,則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]; 
    } 
} 
0

發佈我的另一種解決方案,因爲我有完全相同的問題,因爲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; 

} 
0

接受的解決方案不啓用按鈕。它實際上改變了安全文本字段上的退格行爲。

這個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 
} 
0

斯威夫特 - 3

let text = textField.text 
     if string.length == 0{ 
      textField.deleteBackward() 
      if textField.text?.length != (text?.length)! - 1 { 
      //deleted whole word 

      } 
      return false // this is important 

     } 
0

swift3.0 - 它的工作原理。

FUNC文本框(_文本框:的UITextField,shouldChangeCharactersIn範圍:NSRange,replacementString字符串:字符串) - >布爾{

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 
} 
0

我面臨同樣的問題,我想,當退格是要檢測清除所有字符,以便我可以禁用屏幕上的其他按鈕。所以我就是這樣實現的。

-(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 
} 

這個解決方案是不同的,因爲它可以讓你採取行動時,安全領域中已經有一些文本,你想編輯它,而在上述接受的答案,你會在塊就算去你在編輯一個字段的時候回退了空格,它不會清除整個文本,而只會刪除一個字符。使用我的方法,你可以選擇在這種特殊情況下采取行動,但它也不會影響本地流量。

相關問題