swift - 如何比较一个函数以便触发另一个函数

标签 swift function

我有一个验证字段的函数。正常工作:

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

}

我已经尝试了几个小时,但仍然不知道该怎么做。 实际上,如果所有字段都有效,则不会触发另一个函数的错误。无论我试图与之比较,总是返回 true,即使它完成并且所有文件都经过验证。所以我无法触发另一个功能。

这是 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
        }

    }

最佳答案

你需要修改这个方法

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
    }

}

还有这个

    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
    }

}

希望对你有帮助

关于swift - 如何比较一个函数以便触发另一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45069224/

相关文章:

swift - 读取核心数据实体的属性值

ios - Swift 3 - 对象类型 'RealmSwiftObject' 不受 Realm 异常管理

c - Do-while 循环 - 终止条件有什么问题?

记住以前记录的MySQL查询功能

Excel - 过滤函数 - 排除值范围

ios - 如何一次关闭两个模态视图 Controller ?

ios - UITableViewCell 通过滑动而不是通过点击选择

ios - 如何为 Swift 实现 AVCam

c++ - 在 C++ 中将函数作为参数传递给模板?

matlab - 新的 MATLAB 版本用类方法覆盖了我的函数。我还能调用我的函数吗?