2016-07-24 65 views
1

我已經登錄控制器,其中有兩個文本框:變化的UITextField響應

  • 訪問卡
  • 密碼

最大長度爲門禁卡是9和一旦用戶鍵入第九個數字,它應該出現在訪問卡字段上,然後光標需要移動到密碼字段。

在我的代碼中,當用戶單擊以輸入第九個數字但光標未出現並且光標移至密碼字段時,光標正在移動。

例如:我想輸入「123456789」作爲訪問卡。當我點擊「9」它不會出現,但光標移動到密碼字段:

LoginController.swift:

let ACCESSCARD_MAXLENGTH = 9 
let PASSWORD_MAXLENGTH = 12 
var AccessCardtextFieldLength = 0 
var PasswordTextFieldLength = 0 

class LoginViewController: UIViewController , UITextFieldDelegate { 
    @IBOutlet weak var AccessCardTextField: UITextField! 

    @IBOutlet weak var PasswordTextField: UITextField! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     // calling the function that initialize textFields 
     initializeTextFields() 

    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 

    } 

    // function is used to initialize textFields 
    func initializeTextFields() { 

     // To set the focus on the access card once the view load. 
     AccessCardTextField.becomeFirstResponder() 


     // This must be defined so we can apply the text field functions on it 
     AccessCardTextField.delegate = self 
     PasswordTextField.delegate = self 


     // Define the keyboard type of the textFields. 
     AccessCardTextField.keyboardType = UIKeyboardType.NumberPad 
     PasswordTextField.keyboardType = UIKeyboardType.ASCIICapable 

    } 


func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool { 


     AccessCardtextFieldLength = (textField.text?.characters.count)! + string.characters.count 
     PasswordTextFieldLength = (textField.text?.characters.count)! + string.characters.count 
     if (textField == AccessCardTextField){ 
      for i in 0..<ACCESSCARD_MAXLENGTH{ 
       if (AccessCardtextFieldLength == ACCESSCARD_MAXLENGTH){ 
        PasswordTextField.becomeFirstResponder() 
       } 
       else{ 
        return true 
       } 
       return false 
      } 

     } 
     if (textField == PasswordTextField){ 
      return PasswordTextFieldLength <= PASSWORD_MAXLENGTH ? true : false 

     } 

     return true 
    } 
} 
+0

當您添加斷點時,您的代碼執行了'PasswordTextField.becomeFirstResponder()'嗎? – jo3birdtalk

+0

是的,它添加了斷點到PasswordTextField.becomeFirstResponder(),但代碼沒有停止。 –

回答

1

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool當它返回true只會更新。在這種情況下,您正在更改firstResponder,因此它不會更新。

我的建議是對這種情況使用add target。這是你可以做的:

override func viewDidLoad() { 
    super.viewDidLoad() 

    // calling the function that initialize textFields 
    initializeTextFields() 
    accessCardTextField.addTarget(self, action: #selector(LoginViewController.accessCardTextFieldChanged(_:)), forControlEvents: .EditingChanged) 
} 

func accessCardTextFieldChanged(textField: UITextField) { 
    if textField.text?.characters.count == ACCESSCARD_MAXLENGTH { 
     modelTextField.becomeFirstResponder() 
    } 
} 

這樣,它可以節省你很多行代碼。最重要的是,只有accessCardTextField被更改纔會被調用。你可以做另一個功能來分別檢查你的密碼文本框的長度。另外,我從AccessCardTextField更名爲accessCardTextField。建議以小寫開頭的變量。

+0

你的代碼給出了一個錯誤「不能調用非函數類型UITextField的值」 –

+0

對不起,當我用你的變量替換它時,我不小心刪除了'.addTarget'。我已經更新了它,試試這個。 –

+0

謝謝@Zack Kwan,它的工作原理是 –

0

不太確定這是否可行,但請嘗試編輯您的if語句內容。

if (AccessCardtextFieldLength == ACCESSCARD_MAXLENGTH){ 
     if (textField == AccessCardTextField) { 
      textField.resignFirstResponder() 
      PasswordTextField.becomeFirstResponder() 
    } 
} 
+0

,但它仍然表現相同。 –

0

在這種情況下

if (AccessCardtextFieldLength == ACCESSCARD_MAXLENGTH){ 
    PasswordTextField.becomeFirstResponder() 
    **return true** 
} 
else{ 
    return true 
} 

在返回FLASE這就是爲什麼它不會顯示你的最後一個字符。