我已经登录控制器,其中有两个文本框:变化的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(),但代码没有停止。 –