フィールドを検証する関数があります。正常に動作します:関数を比較して別の関数をトリガーする方法
//MARK: Validate everything before saving to database
func validateEverything() {
checkIfFieldsAreEmpty(textField: accountSetupST2.firstNameTextField, alertMessage: "First name field is empty", view: accountSetupST2.nameView)
checkIfFieldsAreEmpty(textField: accountSetupST2.lastNameTextField, alertMessage: "Last name field is empty", view: accountSetupST2.nameView)
checkIfFieldsAreEmpty(textField: accountSetupST2.dobTextField, alertMessage: "You DOB can not be empty", view: accountSetupST2.dobView)
checkIfTextViewsAreEmpty(textView: accountSetupST2.aboutMeTextView, alertMessage: "You need to write something about your self", view: accountSetupST2.aboutMeView)
}
私は今、達成しようとしていますどのようなことは、この関数がtrueを返した場合、他の機能を起動することです。
func saveToDatabaseAndGoToHomePage() {
print("Save and GO Home")
if validateEverything() != nil {
print("Good to go")
} else {
print("WRONGGGG")
// add code to database here
}
}
私は数時間のためにしようとしたが、まだ把握することはできませんされてきましたどのようにそれを行う。 実際には、すべてのフィールドが有効で、エラーなしで別の機能をトリガーする場合。私が比較しようとしているものは、それが終了してすべての提出物が有効になったとしても、常に真実を返します。だから私は別の機能を引き起こすことはできません。
これはcheckIfFieldsするための機能であるが空である:
> extension UIViewController {
//check if text field is empty
func checkIfFieldsAreEmpty(textField: UITextField, alertMessage: String, view: UIView) {
if (textField.text?.isEmpty)! {
let alertEmptyFields = UIAlertController(title: "Warning", message: alertMessage, preferredStyle: .alert)
let action = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alertEmptyFields.addAction(action)
self.present(alertEmptyFields, animated: true, completion: nil)
view.layer.borderWidth = 0.5
view.layer.borderColor = UIColor.red.cgColor
} else {
view.layer.borderWidth = 0
}
}
は 'validateEverything'は何も...' checkIfFieldsAreEmpty'はブール値を返すため – Alexander
あなたの方法を返さないのに役立ちますか? –
それはBoolを返さない...私は秒で私の質問に関数を配置します。 –