Trenton Tyler
Trenton Tyler

Reputation: 23

Disable button until fields are full in Swift

I want to create a login page where users can not hit the login button until the fields are filled out. I have seen how to do it in objective-c but I am looking to do it in swift and I'm not quite sure how to do so! I have this so far and this is in my loginViewController.swift file.

if username.isEmpty || password.isEmpty {
        loginButton.userInteractionEnabled = false
     } else {
        loginButton.userInteractionEnabled = true
     }

It checks for the empty fields but the button does not become active in real time when the fields are filled in. How can I do this?

This is the code in my file which is in a function for loginTapped.

@IBAction func loginTapped(sender: AnyObject) {

    let username = usernameField.text
    let password = passwordField.text

    /** [[Check for empty fields]] */

     if username.isEmpty || password.isEmpty {
        var errorFieldBorderColor = UIColor(red: 255/255, green: 0/255, blue: 0/255, alpha: 1.0)
        usernameField.layer.borderWidth = 1.0
        usernameField.layer.cornerRadius = 5.0
        usernameField.layer.borderColor = errorFieldBorderColor.CGColor
        usernameField.textColor = UIColor(red: 255/255, green: 0/255, blue: 0/255, alpha: 1.0)


        } else {
        loginButton.userInteractionEnabled = true
     }

    PFUser.logInWithUsernameInBackground(username, password:password) {
        (user: PFUser?, error: NSError?) -> Void in
        if user != nil {
            // Do stuff after successful login.
        } else {
            // The login failed. Check error to see why.
        }
    }
}

Upvotes: 0

Views: 2313

Answers (2)

Waffeln
Waffeln

Reputation: 213

textfield outlets:

@IBOutlet weak var loginUsernameTextField: UITextField!

@IBOutlet weak var loginPasswordTextField: UITextField!

@IBOutlet weak var loginButtonOutlet: UIButton!

in viewDidload:

NSNotificationCenter.defaultCenter().addObserver(self, selector: "textChanged:", name: UITextFieldTextDidChangeNotification, object: nil)
loginButtonOutlet.enabled = false

then add the function and code

func textChanged(sender: NSNotification) {
    if loginUsernameTextField.hasText() && loginPasswordTextField.hasText() {
        loginButtonOutlet.enabled = true
    }
    else {
        loginButtonOutlet.enabled = false
    }
}

i think this should do the work

Upvotes: 5

Samurai Girl
Samurai Girl

Reputation: 998

I think there are at least two ways for doing this.

First - the easy one could be to put validation code inside the login button action. In this case, the button would not be disabled or greyed out and it would receive user interaction. Then some validation code would be executed and corresponding messages would be generated for the user.

Second - In case you want to have your button greyed out and enable only when all input is valid, you will have to implement UITextFieldDelegate methods and keep track of valid/invalid inputs each time any text field loses focus (for example), so you can get notified, when to enable the button.

Upvotes: 0

Related Questions