iosswiftbuttoncanactivate

Check the text field, text view, and image view to activate the button in swift


I am trying to implement the sign up view. This view uses an image picker to check if an image is set, and if all three text fields are not empty and the text view below is not empty, I want to implement a view in which the next button is activated.

I have implemented button activation with three text fields via stackoverflow, but I don't know how to check image view and text view together.

I've tried it for almost a month and I can't think of it in my mind. Any ideas? thanks!

code:

// Properties
@IBOutlet weak var IDTextField: UITextField!
@IBOutlet weak var passwordTextField: UITextField!
@IBOutlet weak var checkPasswordTextField: UITextField!
@IBOutlet weak var textView: UITextView!
@IBOutlet weak var nextButton: UIButton!

// ImagePicker
lazy var imagePicker: UIImagePickerController = {
    var picker = UIImagePickerController()
    picker.sourceType = .photoLibrary
    picker.delegate = self
    picker.allowsEditing = true
    return picker
}()

@IBOutlet weak var imageView: UIImageView!

override func viewDidLoad() {
    super.viewDidLoad()
    IDTextField.addTarget(self, action: #selector(editingChanged(_:)), for: .editingChanged)
    passwordTextField.addTarget(self, action: #selector(editingChanged(_:)), for: .editingChanged)
    checkPasswordTextField.addTarget(self, action: #selector(editingChanged(_:)), for: .editingChanged)
    
}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    if let image: UIImage = info[UIImagePickerController.InfoKey.editedImage] as? UIImage {
        self.imageView.image = image
    }
    self.dismiss(animated: true, completion: nil)
}

// check TextField
@objc func editingChanged(_ textField: UITextField) {
    if textField.text?.count == 1 {
        if textField.text?.first == " " {
            textField.text = ""
            return
        }
    }
    guard
        let ID = IDTextField.text, !ID.isEmpty,
        let PW = passwordTextField.text, !PW.isEmpty,
        let CheckPW = checkPasswordTextField.text, !CheckPW.isEmpty,
        PW == CheckPW
    else {
        nextButton.isEnabled = false
        return
    }
    nextButton.isEnabled = true
}

In this picture, I want to activate the button by checking the status of the image view, text field, and text view. But I didn't know how to check all three at once. thank for help

sign up view image: sign up view image


Solution

  • What you want is to check if the nextButton should be enabled for each change in the textFields and the imageView. If you have one function that handles the checking, you can call that from wherever a check is required and keep your code clean.

    I would create a function checking all required fields. For example:

    /**
     *  This function goes through all fields and checks if they are in the accepted state;
     * - IDTextField should not be empty
     * - passwordTextField and checkPasswordtextField should not be empty and should match
     * - imageView should contain an image
     */
    private func toggleNextButton()
    {
        if
            !self.IDTextField.isEmpty,
            let password = self.passwordTextField, password.count > 0,
            let checkPassword = self.checkPasswordTextField, checkPassword.count > 0,
            password == checkPassword,
            self.imageView.image != nil
        {
            self.nextButton.isEnabled = true
        }
        else
        {
            self.nextButton.isEnabled = false
        }
    }
    

    Call this function on any change in the fields. Your editingChanged would become:

    // check TextField
    @objc func editingChanged(_ textField: UITextField) 
    {
        if textField.text?.count == 1 {
            if textField.text?.first == " " {
                textField.text = ""
                return
            }
        }
    
        self.toggleNextButton()
    }
    

    And your imagePickerController would become:

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) 
    {
        if let image: UIImage = info[UIImagePickerController.InfoKey.editedImage] as? UIImage {
            self.imageView.image = image
        }
    
        self.toggleNextButton()
    }