2016-07-29 108 views
3

我在一行中有一系列textfields。每個textfield限制爲兩個字符。Swift UITextField deleteBackward Issue

我的問題是,我想刪除所有的字符,並通過所有的textfields回去,直到我到達第一個textfield

我有子類文本字段,並覆蓋了deleteBackward方法,以確定是否在文本框文本爲空。如果是這樣,請將響應者更改爲以前的文本字段。

這是所有罰款,但是當在文本框一個字符,按退格鍵,字符被刪除,並且響應被改變以前的文本字段。

這是因爲我想重點留在該文本框是有其性格中刪除,如果刪除鍵被再次按下,那麼響應者應該改變以前的文本框不正確的行爲。

我糾正了這個我設置的文本字段的shouldChangeCharacters委託方法的標誌。該標誌設置爲true,如果...

if (newLength == 0 && currentLength == 0) || currentLength == 0 { 
    textFieldEmpty = true 
} else { 
    textFieldEmpty = false 
} 

這工作得很好,但是當文本框爲空,按退格鍵,此委託方法是不是叫什麼名字?

是否有人已經解決了類似的問題,他們可能會採取什麼辦法只是徘徊?

感謝

回答

0

我也有類似的情況,但在每一個文本字段中單個字符。我還使用自定義textField中的deleteBackward來實現此目的。

試試這個代碼:

import UIKit 
protocol CustomTextFieldDelegate{ 
func textFieldDidDelete() 
} 
class CustomTextField: UITextField { 
    var myCustomTextFieldDelegate : CustomTextFieldDelegate! 
    override func deleteBackward() { 
    super.deleteBackward() 
    myCustomTextFieldDelegate.textFieldDidDelete() 
} 
} 

而且這是你應該如何實現deleteBackward方法:

func textFieldDidDelete() { 
    print("Entered Delete"); 
    print("Tag!!\(currentActiveTextField.tag)") 
    var previousTag = 0 
    if currentActiveTextField.text == ""{ 
     previousTag = currentActiveTextField.tag - 1 
    }else{ 
    previousTag = currentActiveTextField.tag 
    } 
    let previousResponder = currentActiveTextField.superview?.viewWithTag(previousTag) 
    if(previousTag > 0){ 
     let previousTextField = previousResponder as! CustomTextField 
     previousTextField.text = "" 
     currentActiveTextField = previousTextField 
     previousResponder?.becomeFirstResponder() 
    } 
} 

而且在範圍方法shouldChangeCharacters,不要忘記設置您的currentlyActiveTextField爲文本字段你想編輯。檢查下面的代碼,可能會有所幫助。我的文本字段有標籤1,2,3,4。

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool { 
    currentActiveTextField = textField as! CustomTextField 
    let nextTag = textField.tag + 1; 
    // get next responder 
    var nextResponder = textField.superview?.viewWithTag(nextTag); 


    // On inputing value to textfield 
    if (textField.text?.characters.count < 1 && string.characters.count > 0){ 

     if (nextResponder == nil){ 
      nextResponder = textField.superview?.viewWithTag(1); 
     } 
     textField.text = string; 

     if nextTag == 5 { 
      self.digitFourTextField.resignFirstResponder() 
      return true; 
     } 

     nextResponder?.becomeFirstResponder(); 
     return false; 
    } 
    if (textField.text?.characters.count >= 1 && string.characters.count == 0){ 
     // on deleteing value from Textfield 
     let presentTag = textField.tag; 
     // get next responder 
     var presentResponder = textField.superview?.viewWithTag(presentTag); 

     if (presentResponder == nil){ 
      presentResponder = textField.superview?.viewWithTag(1); 
     } 
     textField.text = ""; 
     presentResponder?.becomeFirstResponder(); 
     return false; 
    } 

    let currentCharacterCount = textField.text?.characters.count ?? 0 
    let newLength = currentCharacterCount + string.characters.count - range.length 
    if newLength > 1 { 
     textField.text = string 
     if (nextResponder != nil){ 
      nextResponder?.becomeFirstResponder(); 
     } 

    } 
    let returnVal = newLength <= 1 
    return returnVal; 
}