Im trying to make a login screen similar to the lock screen with 4 textField. The problem i'm facing is with changing the focus from one text field to the next.
- when i say
secondTextField.becomeFirstResponder()
, the value of the firstTextField gets copied to the secondTextField - i'm using a decimal pad - keyboard type
Here is what i have done so far and i can't find a way around to fix the problem :
- Using UITextFieldDelegate
- made IBOutlet connection for all the textField
firstText.delegate = self secondText.delegate = self thirdText.delegate = self firstText.becomeFirstResponder()
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool { var newLength = countElements(textField.text) + countElements(string) - range.length
if newLength == 1 { if textField == firstText{ changeFocus(textField) }else if textField == secondText { changeFocus(textField) }else if textField == thirdText { changeFocus(textField) } } return newLength <= 1
}
func changeFocus(nextFocus: UITextField){
if nextFocus == firstText { firstText.resignFirstResponder() secondText.becomeFirstResponder() } else if nextFocus == secondText { secondText.resignFirstResponder() thirdText.becomeFirstResponder() } else if nextFocus == thirdText { thirdText.resignFirstResponder() }
}
How can i do this correctly, any help and suggestion please.