Thursday, October 29, 2015

UI With Text Field Checklist

Keyboard Return Key
    - Set UITextField delegate in the main storyboard
    - implement the protocol UITextFieldDelegate or UITextViewDelegate in the view controller
    - override the function 
    func textFieldShouldReturn(textField: UITextField) -> Bool

 Dismiss Keyboard on Background Tap
    - Change the view class from UIView to UIControl
    - Allow User Interaction should be checked
    - Create an action touch down on the UIControl
    - On the action for touch down, invoke:
    @IBAction func backgroundTouchDown(sender: AnyObject) {
        view.endEditing(true)
    }


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)