2015-10-20 47 views
2

因此,我正在開發一個應用程序註冊屏幕。我試圖檢查註冊屏幕上的每個字段是否爲空,如果是,則在標籤中向用戶顯示錯誤消息。我一直在使用別人-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顯示消息「名失蹤」。輸入名字並按下注冊按鈕什麼也不做。我希望它變成「姓氏遺失」,但它保持「缺少名字」。

+0

所以你認爲'(self.firstNameField。文本?.isEmpty)!= nil'會以某種方式被錯誤? – matt

+0

你在哪裏稱此驗證碼?當你改變任何文本字段或點擊註冊時,它應該一致地被調用。 – Shripada

+0

在Swift 2中,我會這樣做:if firstNameField.text.character.count == 0 {.. –

回答

7

發生這種情況的原因是因爲您正在檢查self.field.text?.isEmpty != nil。您應該檢查(self.field.text?.isEmpty ?? true)

基本上,您正試圖獲取字段中的文本,如果沒有文本,則返回nil。通過使用field.text?,您根據field.text是否爲零來訪問下一個變量nil。所以,當沒有文字時,field.text == nil,做field.text?.isEmpty將總是返回零。

當有文本時,field.text?.isEmpty不會爲零,並且始終爲假,但nil != false,所以該語句將始終返回false

爲了解決這個問題,你應該檢查

if(self.field.text?.isEmpty ?? true) 

這基本上意味着

if((self.field.text?.isEmpty == nil ? true : self.field.text?.isEmpty)) 

基本上,這將由於返回true,如果field.text == nil(這將使field.text?.isEmpty零,使得結果true??運算符),並且如果field.text != nil || field.text.isEmpty也將返回真。只有self.field.text != nil && !self.field.text.isEmpty纔會返回false。

另一種方式來寫這個聲明將是

if(self.field.text == nil || self.field.text!.isEmpty) 
+0

工作,謝謝! –

+0

沒問題!如果您想將此答案標記爲正確答案,您可以點擊投票計數下的複選標記 – Jojodmo

1

嘗試了這一點:

if self.firstNameField.text?.isEmpty { 
    errorLabel.text = "first name missing" 
    errorLabel.hidden = false 
} 

else if self.lastNameField.text?.isEmpty { 
    errorLabel.text = "last name missing" 
    errorLabel.hidden = false 
} 

else if self.emailField.text?.isEmpty { 
    errorLabel.text = "email missing" 
    errorLabel.hidden = false 
} 

else if self.passwordField.text?.isEmpty { 
    errorLabel.text = "password missing" 
    errorLabel.hidden = false 
} 

else if self.confirmPasswordField.text?.isEmpty { 
    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 
} 
1

嘗試了這一點:

if self.firstNameField.text! == "" { 
    errorLabel.text = "first name missing" 
    errorLabel.hidden = false 
} 
else if self.lastNameField.text! == "" { 
    errorLabel.text = "last name missing" 
    errorLabel.hidden = false 
} 
else if self.emailField.text! == "" { 
    errorLabel.text = "email missing" 
    errorLabel.hidden = false 
} 
else if self.passwordField.text! == "" { 
    errorLabel.text = "password missing" 
    errorLabel.hidden = false 
} 
else if self.confirmPasswordField.text! == "" { 
    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 
}