如何用boolean设置func



我在表单屏幕上验证文本字段时遇到了问题。即使其中一个文本字段为空,我也不想允许抛出请求。我想显示空文本字段的警告消息。如果所有条件都满足,我想提交一份申请。

func isValid() -> Bool {
if nameTextField.text?.isEmpty == false  {
nameWarningLabel.isHidden = true
nameTextField.layer.borderColor = R.color.rgb_222_222_222()?.cgColor


}
else {
nameWarningLabel.isHidden = false
nameTextField.layer.borderColor = R.color.rgb_249_36_23()?.cgColor

}
if surnameTextField.text?.isEmpty == false {
surnameWarningLabel.isHidden = true
surnameTextField.layer.borderColor = R.color.rgb_222_222_222()?.cgColor

}
else {
surnameWarningLabel.isHidden = false
surnameTextField.layer.borderColor = R.color.rgb_249_36_23()?.cgColor

}
if cellPhoneTextField.text?.isEmpty == false {
phoneWarningLabel.isHidden = true
cellPhoneTextField.layer.borderColor = R.color.rgb_222_222_222()?.cgColor


}
else {
phoneWarningLabel.isHidden = false
cellPhoneTextField.layer.borderColor = R.color.rgb_249_36_23()?.cgColor

}
if isChecked == true {
agreementCheckBoxButton.layer.borderColor = R.color.rgb_222_222_222()?.cgColor

}
else {

agreementCheckBoxButton.layer.borderColor = R.color.rgb_249_36_23()?.cgColor

}

if  isChecked == true || (nameTextField.text?.count ?? 0) > 2 || (surnameTextField.text?.count ?? 0) > 2 || (cellPhoneTextField.text?.count ?? 0) < 10  {
return true
}
else {
return false
}
}

如果此函数返回true或false,我将向服务我正在下面与您共享代码块

在函数中创建一个BOOL变量。将默认值设置为true,当遇到else条件时,将其设置为false。最后,在函数的末尾,返回BOOL变量的值。如果满足所有正条件,则返回默认值true,否则返回false

func isValid() -> Bool {
//Declare Variable
var sendRequest:Bool = true
//Check Name Text Field
if nameTextField.text?.isEmpty == false  {
nameWarningLabel.isHidden = true
nameTextField.layer.borderColor = R.color.rgb_222_222_222()?.cgColor
} else {
nameWarningLabel.isHidden = false
nameTextField.layer.borderColor = R.color.rgb_249_36_23()?.cgColor
sendRequest = false
}
//Check Surname Text Field
if surnameTextField.text?.isEmpty == false {
surnameWarningLabel.isHidden = true
surnameTextField.layer.borderColor = R.color.rgb_222_222_222()?.cgColor
} else {
surnameWarningLabel.isHidden = false
surnameTextField.layer.borderColor = R.color.rgb_249_36_23()?.cgColor
sendRequest = false
}
//Check Cell Phone Text Field
if cellPhoneTextField.text?.isEmpty == false {
phoneWarningLabel.isHidden = true
cellPhoneTextField.layer.borderColor = R.color.rgb_222_222_222()?.cgColor   
} else {
phoneWarningLabel.isHidden = false
cellPhoneTextField.layer.borderColor = R.color.rgb_249_36_23()?.cgColor
sendRequest = false
}
//Check Agreement Check Box
if isChecked == true {
agreementCheckBoxButton.layer.borderColor = R.color.rgb_222_222_222()?.cgColor  
} else {   
agreementCheckBoxButton.layer.borderColor = R.color.rgb_249_36_23()?.cgColor
sendRequest = false      
}

//Return Variable
return sendRequest 
}

相关内容

  • 没有找到相关文章

最新更新