简体   繁体   中英

Is it possible to use a UITextFieldDelegate method within a function in swift?

I have a function determines whether a form should be submitted by checking a bool that is returned.

The function:

func signUpViewController(signUpController: PFSignUpViewController!, shouldBeginSignUp info: [NSObject : AnyObject]!) -> Bool {
    var informationComplete = true

    // loop through all of the submitted data
    for (key, value) in info {
        let fieldValue: AnyObject? = value

        if (!fieldValue || fieldValue?.length == 0) { // check completion
            informationComplete = false;
            break;
        }
    }

    // is this possible
    textFieldDidEndEditing(textField: UITextField?) {

    }



    // Display an alert if a field wasn't completed
    if (!informationComplete) {

        var alert = UIAlertController(title: "Error", message: "Make sure you fill out all of the fields!", preferredStyle: UIAlertControllerStyle.Alert)
        alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
        self.presentViewController(alert, animated: true, completion: nil)


    }

     PFUser.logOut()
    return informationComplete;
}

Current I check for empty fields but need to check for other things such as amount of text in a text field, whether an email address has been correctly entered.

I want to use textFieldDidEndEditing to check the contents of a text field after it has been left. I will basically use an if statement to do a check then display an error message but also set informtionComplete to false so that the form is not submitted with any errors. I'd probably disable the submit button too.

Out side of the above function I'd do something like this:

override func textFieldDidEndEditing(textField: UITextField!) {
    if textField == self.signUpView.usernameField {
        var length = countElements(self.signUpView.usernameField.text!)
        if length < 2 {
            var alert = UIAlertController(title: "Error", message: "Your company can only have a minimum of 2 characters!", preferredStyle: UIAlertControllerStyle.Alert)
            alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
            self.presentViewController(alert, animated: true, completion: nil)
            self.signUpView.signUpButton.enabled = false
        } else {
            self.signUpView.signUpButton.enabled = true
        }
    }

    if textField == self.signUpView.passwordField {

    }

    if textField == self.signUpView.emailField {

    }

}

Can similar be done within the function or do I need to set up instance vars like I would in objective-c and set the informationComplete var from outside of the signUpViewController function?

Well you always need to call the function on the correct object, in this case the delegate itself. So if the delegate isn't self, you have to call it like

theDelegate.textFieldDidEndEditing(self.signUpView.usernameField)

Note that you also have to specify which textfield it is.

But the real problem here is structural. You are confused in where to check for what. The textFieldShouldEndEditing() and textFieldDidEndEditing() functions are the right place for validating data entry on a specific field. If you need to validate a combination of items, then the validation needs to be done upon saving. But when you do check upon saving, there's no need to call the textField delegate methods as those will have already run.

If you need to know what happened when data was entered in a field, then you use an object variable that you set inside the delegate methods, which you can check from the save method. You should never have to call again directly the delegate methods.

This is what is working for me:

// loop through all of the submitted data
for (key, value) in info {
    let fieldValue: AnyObject? = value

    if (!fieldValue || fieldValue?.length == 0) { // check completion
        informationComplete = false;
        break;
    } else if (countElements(self.signUpView.usernameField.text!) < 2) {
        NSLog("signup field text less than 2")
        // display alert
        informationComplete = false
    } else if (self.signUpView.passwordField) {
        NSLog("password error ")
        // display alert
        informationComplete = false
    }
}

I validate from within the signUpViewController method. I removed the if statement that displays an alert and modified my for loop

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM