【问题标题】:Check if multiple textfields are empty - iOS with Swift检查多个文本字段是否为空 - 带有 Swift 的 iOS
【发布时间】: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 { ..
  • 另外:你听说过循环吗?

标签: ios swift


【解决方案1】:

发生这种情况的原因是因为您正在检查self.field.text?.isEmpty != nil。你应该检查(self.field.text?.isEmpty ?? true)

本质上,您正在尝试获取字段中的文本,如果没有文本,则返回 nil。通过使用field.text?,您将根据field.text 是否为nil 来创建您访问nil 的下一个变量。因此,当没有文本时,field.text == nil,执行field.text?.isEmpty 将始终返回 nil。

当有文本时,field.text?.isEmpty 不会为 nil,永远为 false,而是nil != false,所以语句会永远返回false

要解决这个问题,你应该检查

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

这基本上意味着

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

基本上,如果field.text == nil(这将使field.text?.isEmpty 为零,由于?? 运算符而使结果true),这将返回true,如果field.text != nil || field.text.isEmpty 也将返回true。只有self.field.text != nil && !self.field.text.isEmpty 才会返回false。

写此语句的另一种方式是

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

【讨论】:

  • 没问题!如果您想将此答案标记为正确答案,您可以点击计票下方的复选标记
【解决方案2】:

试试这个:

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
} 

【讨论】:

    【解决方案3】:

    试试这个:

    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
    }
    

    【讨论】:

      【解决方案4】:

      您可以避免 if-else 情况,并在 swift 中使用带有三元条件运算符的一行代码来实现它。

      firstNameField.text?.isEmpty == true ? showError(message: ""first name missing"") : isValid()
      ...
      }
      
      func showError(message : String){
       errorLabel.text = message
       errorLabel.hidden = false
      }
      func isValid(){
        // hide label or anything else you want
      }
      

      【讨论】:

        猜你喜欢
        • 2016-11-04
        • 2021-12-02
        • 1970-01-01
        • 1970-01-01
        • 2021-07-01
        • 2015-12-28
        • 1970-01-01
        • 2014-02-01
        • 1970-01-01
        相关资源
        最近更新 更多