Home > OS >  How to check if UITextField became empty again?
How to check if UITextField became empty again?

Time:10-20

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.

CodePudding user response:

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

CodePudding user response:

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

CodePudding user response:

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 :)

  • Related