因此,我正在開發一個應用程序註冊屏幕。我試圖檢查註冊屏幕上的每個字段是否爲空,如果是,則在標籤中向用戶顯示錯誤消息。我一直在使用別人-IFS檢查是否有多個文本框是空的 - 使用Swift的iOS
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。文本?.isEmpty)!= nil'會以某種方式被錯誤? – matt
你在哪裏稱此驗證碼?當你改變任何文本字段或點擊註冊時,它應該一致地被調用。 – Shripada
在Swift 2中,我會這樣做:if firstNameField.text.character.count == 0 {.. –