I'm not to familiar with the delegate for "Use String password" but if the user is choosing their own password you need to check what the user types in the password textField and verify that it meets your criteria.
To do that you need to check what's typed into the password textField using it's target .editingChanged
. As the user types whatever goes inside of it will either enable or disable the button. Look at step #3 and step #4. Step 4 is what will toggle and decide to wither enable or disable the button.
Another thing to take note of is that when the vc first appears the button should be disabled
and then once the password textField data is valid then enable then button. Step #1 and Step #4
For example in my app if a user enters in all blank spaces inside the password textField the signUp button will be disabled but if they enter in valid data the the button will be enabled.
UPDATE I added in step 3A and 3B in viewDidLoad
afterwards because as @DawsonToth correctly pointed out in the comments if the user chooses a strong password the next button will be enabled. But if they then decided to pick Choose My Own Password the passwordTextField will clear but the next button will still be enabled. I didn't account for that.
You need to add a KVO observer
to the passwordTextField's "text" keypath
so that every time the passwordTextField's text changes handleTextInputChanged() will get called thus disabling the next button once the text is cleared.
// 1. create a signUp button and make sure it is DISABLED and the color is .lightGray to signify that. In step 4 if the data inside the passwordTextField is valid I enable then button and change the color to blue
let signUpButton: UIButton = {
let button = UIButton(type: .system)
button.isEnabled = false // ** this must be set as false otherwise the user can always press the button **
button.setTitle("SignUp", for: .normal)
button.setTitleColor(UIColor.white, for: .normal)
button.backgroundColor = UIColor.lightGray
button.addTarget(self, action: #selector(signUpButtonTapped), for: .touchUpInside)
return button
}()
// 2. create the password textField and set its delegate in viewDidLoad. For eg self.passwordTextField.delegate = self
let passwordTextField: UITextField = {
let textField = UITextField()
textField.placeholder = "Enter Password"
textField.autocapitalizationType = .none
textField.returnKeyType = .done
textField.isSecureTextEntry = true
// 3. add the target for .editingChanged to check the textField
textField.addTarget(self, action: #selector(handleTextInputChanged), for: .editingChanged)
return textField
}()
override func viewDidLoad() {
super.viewDidLoad()
passwordTextField.delegate = self // if this is programmatic make sure to add UITextFieldDelegate after the class name
// 3A. Add a KVO observer to the passwordTextField's "text" keypath
passwordTextField.addObserver(self, forKeyPath: "text", options: [.old, .new], context: nil)
}
// 3B. call the observer
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if keyPath == "text" {
handleTextInputChanged()
}
}
// 4. this checks what's typed into the password textField from step 3
@objc fileprivate func handleTextInputChanged() {
let isFormValid = !isPasswordTextFieldIsEmpty() // if the textField ISN'T empty then the form is valid
if isFormValid {
signUpButton.isEnabled = true
signUpButton.backgroundColor = .blue
} else {
signUpButton.isEnabled = false
signUpButton.backgroundColor = .lightGray
}
}
// 5. create a function to check to see if the password textField is empty
func isPasswordTextFieldIsEmpty() -> Bool {
// 6. this checks for blank space
let whiteSpace = CharacterSet.whitespaces
// 7. if the passwordTextField has all blank spaces in it or is empty then return true
if passwordTextField.text!.trimmingCharacters(in: whitespace) == "" || passwordTextField.text!.isEmpty {
return true
}
return false // if it has valid characters in it then return false
}
// 8. target method from step 1
@objc func signUpButtonTapped() {
// run firebase code
}