How to disable the button until the text in all UITextFields is entered quickly?

Do I need to disable the save button until the text is entered in all the necessary text fields? I am developing an application in Swift and I have found many answers in Objective-c. Since I have absolutely knowledge of Objective-c, I cannot understand what this means.

Does anyone have a solution for this that can be made in Swift?

I know how to enable / disable a button. I also know how to check if a text field is empty. I'm just not sure how to make my code always check if it is empty or not. I tried the while loop, but, as I expected, it all froze.

+1
source share
3

:

class ViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet weak var textField: UITextField!
    @IBOutlet weak var button: UIButton!

    //Need to have the ViewController extend UITextFieldDelegate for using this feature
    func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {

        // Find out what the text field will be after adding the current edit
        let text = (textField.text as NSString).stringByReplacingCharactersInRange(range, withString: string)

        if !text.isEmpty{//Checking if the input field is not empty
            button.userInteractionEnabled = true //Enabling the button
        } else {
            button.userInteractionEnabled = false //Disabling the button
        }

        // Return true so the text field will be changed
        return true
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        //Setting the Delegate for the TextField
        textField.delegate = self
        //Default checking and disabling of the Button
        if textField.text.isEmpty{
            button.userInteractionEnabled = false // Disabling the button
        }
    }
}

+3

, , , , :

  • viewController
  • didEndEditing ,

, , .

+2

Use the text field delegate methods (or target action template) to verify the conditions necessary for the user to continue. If they are completed, turn on the button.

+1
source

All Articles