所以我在我的应用程序中注册了表单,但是有问题。这是我的代码:
@IBAction func registerButtonAction(sender: AnyObject) {
if (fullnameTextField.text == ""){
self.fullnameTextField.shake()
}
if (usernameTextfield.text == ""){
self.usernameTextfield.shake()
}
if (usernameTextfield.text?.characters.count < 6){
usernameTextfield.shake()
}
if (emailTextfield.text == ""){
self.emailTextfield.shake()
}
if (passwordTextfield.text == ""){
self.passwordTextfield.shake()
}
if (retypepasswordTextfield.text == ""){
self.retypepasswordTextfield.shake()
}
else{
registerButton.setTitle("Registering user...", forState: UIControlState.Normal)
registerButton.alpha = 0.20
registerButton.enabled = false
registerUser()
}
}
首先:我可以更容易地将此代码缩小吗?第二,如果所有UITextField都有文字,我希望用户只能启动registerUser()
,而usernameTextField
和passwordTextField
的字符数超过6.还有如何修复passwordTextField
和注册前retypepasswordTextfield
有相同的文字吗?有什么建议吗?
答案 0 :(得分:1)
我认为这有点好看:
@IBAction func registerButtonAction(sender: AnyObject) {
let validInput = true
let allTextFields = [fullnameTextField, usernameTextfield, emailTextfield, passwordTextfield, retypepasswordTextfield]
for textField in allTextFields {
if textField.text == "" {
validInput = false
textField.shake()
}
}
let theTextFieldsWithMin6Characters = [usernameTextfield, passwordTextfield]
for textField in theTextFieldsWithMin6Characters {
if textField.text?.characters.count < 6 {
validInput = false
textField.shake()
}
}
if passwordTextfield.text != retypepasswordTextfield.text {
validInput = false
retypepasswordTextfield.shake()
}
if validInput {
registerButton.setTitle("Registering user...", forState: UIControlState.Normal)
registerButton.alpha = 0.20
registerButton.enabled = false
registerUser()
}
}