In this workshop you learn how to dismiss the keyboard that appear on the device screen. When you click the Text View object a keyboard appear at the bottom of the screen. In this workshop you learn how to hide the keyboard when the user taps the device background.

Before you begin, I assume you’ve done the following:

Conform The ViewController Class

Hiding the device keyboard is a multi step process. The first thing you have to do is conform the ViewController class to the UITextViewDelegate protocol. The reason why you have to do that is because you will be implementing a delegate method of the UITextViewDelegate protocol in the ViewController class. Here’s how to conform the ViewController class to the UITextViewDelegate protocol

Click the ViewController.swift file and replace the class signature with this:

class ViewController: UIViewController, UITextViewDelegate {

Implement The Text View Delegate Method

The last thing you have to do is implement this delegate method of the UITextViewDelegate protocol in the ViewController.swift class file. It is fired when you tap the iOS device’s background. In our case the iOS device is the iPhone or iPad Simulator.

override func touchesBegan(touches: NSSet!, withEvent event: UIEvent!) {
  outputTextView.endEditing(true)
}

Run The App in The iOS Simulator

That’s it. Now run the Swift Variable application in the iPhone 4 Simulator and click the Text View to make the keyboard appear on the simulator’s screen-see image below. Click the simulator’s background or the Image View control and the keyboard will disappear. Click the Text View to make the keyboard appear again.
workshop9-fig1

Tags:

No responses yet

Leave a Reply

UIDocument Demystified: A Step-by-step Guide on Local and iCloud Document Storage

 

Archives