2017-07-12 3 views
1

フィールドを検証する関数があります。正常に動作します:関数を比較して別の関数をトリガーする方法

//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 
     } 

    } 
+1

は 'validateEverything'は何も...' checkIfFieldsAreEmpty'はブール値を返すため – Alexander

+0

あなたの方法を返さないのに役立ちますか? –

+0

それはBoolを返さない...私は秒で私の質問に関数を配置します。 –

答えて

1

あなたがこの方法

extension UIViewController { 

//check if text field is empty 
func checkIfFieldsAreEmpty(textField: UITextField, alertMessage: String, view: UIView) ->Bool { 
    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 
     return false 
    } else { 
     view.layer.borderWidth = 0 
     return true 
    } 

} 

この1

func validateEverything() ->Bool{ 
      var validTextFields = 0 
      validTextFields += checkIfFieldsAreEmpty(textField: accountSetupST2.firstNameTextField, alertMessage: "First name field is empty", view: accountSetupST2.nameView) ? 1 : 0 

      validTextFields += checkIfFieldsAreEmpty(textField: accountSetupST2.lastNameTextField, alertMessage: "Last name field is empty", view: accountSetupST2.nameView) ? 1 : 0 

      validTextFields += checkIfFieldsAreEmpty(textField: accountSetupST2.dobTextField, alertMessage: "You DOB can not be empty", view: accountSetupST2.dobView) ? 1 : 0 

      validTextFields += checkIfTextViewsAreEmpty(textView: accountSetupST2.aboutMeTextView, alertMessage: "You need to write something about your self", view: accountSetupST2.aboutMeView) ? 1 : 0 

      return validTextFields == 4 
    } 

を変更する必要がある、あなたは、この

を行うことができます
func saveToDatabaseAndGoToHomePage() { 
    print("Save and GO Home") 

    if validateEverything() { 
     print("Good to go") 
    } else { 
     print("WRONGGGG") 
     // add code to database here 
    } 

} 

希望これは

+0

魅力的な作品...ありがとう! –

関連する問題