Contact Us

Use the form on the right to contact us.

You can edit the text in this area, and change where the contact form on the right submits to, by entering edit mode using the modes on the bottom right. 


Oak Ridge, TN, 37830
United States

Swift-Snips

Keyboard Management

Wade Cantley


So this is a simple bit of code that reflects controls for a text field and a button.

Clicking on the text field brings up the keyboard.  Clicking anywhere outside of the keyboard sends it away
Dependency :
1) there is a view that has a form field and a button.
2) The form field has been set to use the View controller as the delegate (Drag to connect from field to view)  This is important in order to get access to the delegate methods for that field such as “textFieldShouldReturn”.
 


import UIKit

class SecondViewController: UIViewController, UITextFieldDelegate {
    @IBOutlet var txtSomeTextField : UITextField
    
    // Clicking away from the keyboard will remove the keyboard.
    override func touchesBegan(touches: (NSSet!), withEvent event: (UIEvent!)) {
        self.view.endEditing(true)
    }

    // called when 'return' key pressed. return NO to ignore.
    // Requires having the text fields using the view controller as the delegate.
    func textFieldShouldReturn(textField: UITextField!) -> Bool {
       
        // Sends the keyboard away when pressing the return button
        textField.resignFirstResponder()
        return true
       
    }

    @IBAction func btnSomeButton(sender : AnyObject) {
        self.view.endEditing(true)
    }
   

}

If a text field is coded , in you will want to be sure to set the delegate for the field.


var locationField:UITextField!
locationField = UITextField(frame: CGRect(x: 90, y: 227, width: 150, height: 40))
locationField.delegate = self
self.view.addSubview(locationField)

On occasion you might find that the view “endEditing” doesn’t work.  This is likely because the field in question is not within the “view”.  Even when the view controller is the delegate, that isn’t enough.  The field has to be referenced directly.

So, it might look like this.


@IBOutlet weak var addressTextField: UITextField!
self.addressTextField.endEditing(true)

But, this addresses a single field, so what I found works better is to put all the referenced fields in their own function so we are not re-writing the same code when we want to dismiss the keyboard.


//MARK: - Text View Delegates
extension PropertiesViewController: UITextFieldDelegate {
   
     // end editing for any fields listed here
    func endAllEditing(){
        self.addressTextField.endEditing(true)       
    }
   
    // Clicking away from the keyboard will remove the keyboard.
    override func touchesBegan(touches: (NSSet!), withEvent event: (UIEvent!)) {

          //trigger our new function for endEditing all fields.
        endAllEditing()
        println("touchesBegan : end editting")
    }
   
   
    // called when 'return' key pressed. return NO to ignore.
    // Requires having the text fields using the view controller as the delegate.
    func textFieldShouldReturn(textField: UITextField!) -> Bool {
       
        // Sends the keyboard away when pressing the return button
        textField.resignFirstResponder()
        return true
       
    }

}