Tarvo Mäesepp
Tarvo Mäesepp

Reputation: 4533

resignFirstResponder only if all textFields are filled

I want to resignFirstResponder() for one textField only if all textFields are filled. However for some reason it doesn't work. No matter if others are filled, it only checks for the one responder one.

This is what I am trying to do:

//Check wheather fields are filled or not and then enable or disable register button
    func textChanged(_ sender: NSNotification) {
        if usernameField.hasText && emailField.hasText && passwordField.hasText && confirmPasswordField.hasText {
            registerButton.isEnabled = true
            confirmPasswordField.resignFirstResponder()
        }
        else {
            registerButton.isEnabled = false
        }
    }

And I also tried:

func textFieldShouldReturn(textField: UITextField!) -> Bool {
        if usernameField.hasText && emailField.hasText && passwordField.hasText && confirmPasswordField.hasText {
        confirmPasswordField.resignFirstResponder()
            return true
        }
        return false
    }

What I am doing wrong?

Upvotes: 1

Views: 92

Answers (1)

Ajil O.
Ajil O.

Reputation: 6892

This is the code I used

func textFieldShouldReturn(textField: UITextField) -> Bool {
    if text1.hasText() && text2.hasText() && text3.hasText() && text4.hasText(){
        registerButton.enabled = true
        text4.resignFirstResponder()
        return true
    }
    return false
}

I am not sure how you are calling the textChanged function so I can't really figure out what you are trying to do there.

If you want to ensure that a user cannot move to text4 before filling out text1, text2, text3 then use another if block to implement that.

Upvotes: 1

Related Questions