【发布时间】:2015-10-20 04:18:22
【问题描述】:
所以,我正在开发一个应用注册屏幕。我正在尝试检查注册屏幕上的每个字段以查看它是否为空,如果是,则在标签中向用户显示错误消息。我一直在使用一系列 else-ifs
if ((self.firstNameField.text?.isEmpty) != nil) {
errorLabel.text = "first name missing"
errorLabel.hidden = false
}
else if ((self.lastNameField.text?.isEmpty) != nil) {
errorLabel.text = "last name missing"
errorLabel.hidden = false
}
else if ((self.emailField.text?.isEmpty) != nil) {
errorLabel.text = "email missing"
errorLabel.hidden = false
}
else if ((self.passwordField.text?.isEmpty) != nil) {
errorLabel.text = "password missing"
errorLabel.hidden = false
}
else if ((self.confirmPasswordField.text?.isEmpty) != nil) {
errorLabel.text = "password confirmation missing"
errorLabel.hidden = false
}
else if (self.passwordField.text != self.confirmPasswordField.text) {
errorLabel.text = "Passwords don't match, try again!"
errorLabel.hidden = false
}
//omitted what happens if there are no fields missing
现在,当我在所有文本字段为空的情况下运行应用程序时,errorLabel 会显示消息“缺少名字”。输入名字并按下注册按钮没有任何作用。我希望它更改为“缺少姓氏”,但它保持在“缺少名字”。
【问题讨论】:
-
所以你相信
(self.firstNameField.text?.isEmpty) != nil会以某种方式是假的???? -
你在哪里调用这个验证码?每当您更改任何文本字段或点击注册时,都应该始终调用它
-
在 Swift 2 中我会这样做: if firstNameField.text.character.count == 0 { ..
-
另外:你听说过循环吗?