我已經登錄控制器,其中有兩個文本框:變化的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
}
}
當您添加斷點時,您的代碼執行了'PasswordTextField.becomeFirstResponder()'嗎? – jo3birdtalk
是的,它添加了斷點到PasswordTextField.becomeFirstResponder(),但代碼沒有停止。 –