Thursday, October 29, 2015

Enable Save Button on UITextField or UITextView change

1. Create an outlet for the button, to allow toggling its property.
2. On viewDidLoad, set save button enabled to false.
3. Create a function to handle the text change event.
    - If text is empty, set to disabled, else set to enabled.

    func nameChanged(notification: NSNotification) {
        saveButton.enabled = validateFields()
    }

    func validateFields() -> Bool {
        return !nameTextField.text!.isEmpty
    }


3. Register a notification for text change inside the viewDidLoad function

               NSNotificationCenter.defaultCenter().addObserver(self, selector: "nameChanged:", name: UITextFieldTextDidChangeNotification, object: nameTextField)

4. Unregister observer when leaving the page.

        NSNotificationCenter.defaultCenter().removeObserver(self)







No comments:

Post a Comment