skip to Main Content

I’m creating a simple log in VC. let’s ignore the input validation for both username & password. I just want to enable the UIButton when both username’s and password’s UITextField is not empty. And whenever any one of them becomes empty, I want the button to be disabled.

 @IBAction func typingUserName(_ sender: Any) {
    usernameTxtfield.layer.shadowPath = UIBezierPath(rect: usernameTxtfield.bounds).cgPath
    usernameTxtfield.layer.shadowRadius = 2
    usernameTxtfield.layer.shadowOffset = .zero
    usernameTxtfield.layer.shadowOpacity = 0.5
    
    signInIcon.isEnabled = false
}

@IBAction func typingPassword(_ sender: Any) {
    passwordTxtfield.layer.shadowPath = UIBezierPath(rect: passwordTxtfield.bounds).cgPath
    passwordTxtfield.layer.shadowRadius = 2
    passwordTxtfield.layer.shadowOffset = .zero
    passwordTxtfield.layer.shadowOpacity = 0.5
    
    signInIcon.isEnabled = false
}

@IBAction func usernameTxtFieldEditingChnged(_ sender: Any) {
    usernameTxtfield.layer.shadowRadius = 0
    usernameTxtfield.layer.shadowOffset = .zero
    usernameTxtfield.layer.shadowOpacity = 0
    
    
}

@IBAction func passwordEditingChaned(_ sender: Any) {
    passwordTxtfield.layer.shadowRadius = 0
    passwordTxtfield.layer.shadowOffset = .zero
    passwordTxtfield.layer.shadowOpacity = 0
    
    signInIcon.isEnabled = true
}

@IBAction func signInClicked(_ sender: Any) {
    performSegue(withIdentifier: "welcomeVC", sender: signInIcon)
}

As you can see, I’m enabling the button only after the password textfield EditingChanged has been triggered.

3

Answers


  1. You can observe event .editingChanged.

    passwordTxtfield.addTarget(self, action: #selector(passwordEditingChaned), for: .editingChanged)
    usernameTxtfield.addTarget(self, action: #selector(usernameTxtFieldEditingChnged), for: .editingChanged)
    

    And then add check in both methods:

    signInIcon.isEnabled = passwordTxtfield.text?.isEmpty == false && usernameTxtfield.text?.isEmpty == false
    
    Login or Signup to reply.
  2. You have to implement something like this:

    if usernameTxtfield.text != "" && passwordTxtfield.text != "" {
        signInIcon.isEnabled = true
    } else {
        signInIcon.isEnabled = false
    }
    

    You add this piece of code in the action of each UITextField and your are good to go

    Login or Signup to reply.
  3. Connect outlets of textFields and Button to ViewController Class.

    @IBOutlet private weak var usernameTxtfield: UITextField!
    @IBOutlet private weak var passwordTxtfield: UITextField!
    
    @IBOutlet private weak var signInButton: UIButton!
    

    then write your setup function

    private func setupTextFields() {
        // write your TextFields custom setup ...
        // also add delegate lines
        
        usernameTxtfield.delegate = self
        passwordTxtfield.delegate = self
    }
    

    your setupTextFields function to viewDidLoad of VC and also set your button isEnable = false

    override func viewDidLoad() {
        super.viewDidLoad()
    
        setupTextFileds()
    
        setSignInButton(isEnable: false)
    }
    

    and also write func to get textFields.

    private func getTextFields() -> [UITextField] {
        return [usernameTxtfield, passwordTxtfield]
    }
    

    also we need to check if these are valid.

    private func isInputsValid() -> Bool {
        var isValid: Bool = true
        
        let inputs: [UITextField] = getTextFields()
        
        if let input = inputs.first(where: { $0.text?.count == 0 }) {
            debugPrint("(input) is not valid.")
            isValid = false
        }
        
        return isValid
    }
    

    also add func to set Button

    private func setSignInButton(isEnable: Bool) {
        signInButton.isEnabled = isEnable
    }
    

    then write TextField delegate func to understand inputs are changing and change condition of button.

    extension ViewController: UITextFieldDelegate {
    
        func textFieldDidChangeSelection(_ textField: UITextField) {
            setSignInButton(isEnable: isInputsValid())
        }
    }
    

    I hope it was helpful ๐Ÿ™‚

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search