Search code examples
iosswiftxcodexcode8swift4

Disable / Enable a button in Xcode


Hi i'm new with Swift programming. What im trying to do is Disable my button (signIn) in viewDidLoad and only enable when the textfields have text in them. Here's what i've achieved so far. (not much though!)

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    signIn.isEnabled = false

    // Do any additional setup after loading the view, typically from a nib.
}

@IBOutlet weak var emailtxt: UITextField!
@IBOutlet weak var passwordtxt: UITextField!
@IBOutlet weak var signIn: UIButton!

I need help to create a function in signIn that keeps button disabled until text fields (emailtxt & passwordtxt) have text in them and then proceed.

Glad if anyone can sort me. Thanks in advance!


Solution

  • Set ViewController as delegate for emailtxt and passwordtxt like this,

    override func viewDidLoad() {
        super.viewDidLoad()
        signIn.isEnabled = false
    
        emailtxt.delegate = self
        passwordtxt.delegate = self 
    
        // Do any additional setup after loading the view, typically from a nib.
    }
    

    Conform your ViewController to UITextFieldDelegate and enable/disable as the text input is finished,

    extension ViewController: UITextFieldDelegate {
    
        func textFieldDidEndEditing(_ textField: UITextField) {
            if emailtxt.text?.isEmpty == false && passwordtxt.text?.isEmpty == false {
                signIn.isEnabled = true
            } else {
                signIn.isEnabled = false
            }
        }
    }
    

    Here is the fix for your code you shared.

    import UIKit
    extension UIViewController {
    
        func hideKeyboardWhenTappedAround() {
            let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
            tap.cancelsTouchesInView = false
            view.addGestureRecognizer(tap)
        }
    
        @objc func dismissKeyboard() {
            view.endEditing(true)
        }
    }
    
    extension SignInVC: UITextFieldDelegate {
    
        func textFieldDidEndEditing(_ textField: UITextField) {
            if emailtxt.text?.isEmpty == false && passwordtxt.text?.isEmpty == false {
                signIn.isEnabled = true
            } else {
                signIn.isEnabled = false
            }
        }
    }
    class SignInVC: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            signIn.isEnabled = false
            emailtxt.delegate = self
            passwordtxt.delegate = self
            self.hideKeyboardWhenTappedAround()
            // Do any additional setup after loading the view, typically from a nib.
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
        @IBOutlet weak var emailtxt: UITextField!
        @IBOutlet weak var passwordtxt: UITextField!
        @IBOutlet weak var signIn: UIButton!
    }