Dismissing the keyboard can be done by resigning the current text editor as a first responder. In this tutorial, see how to dismiss the keyboard when the user taps away from a text field, both by using touch events and by using delegation.
- [Narrator] Let's say the user starts to enter an item…into the grocery list and wants to cancel out,…and close out the keyboard.…Let's take a look at dismissing the keyboard…in other places in the code.…So let's say,…when they user taps anywhere that's not in the text field,…or the button.…To do that, we can handle when a touch happens by handling…the touches began event in our code.…So, underneath add item,…go right after that close curly brace on line 27,…and it might be near that in your code,…it's on line 27 in my code.…
Go down a few lines,…and then start to type out "touches".…And, as you do that, you should see touchesBegan…becomes highlighted.…It says in the description,…tells this object that one or more new touches occurred…in a view or window.…So press return to have Xcode finish that for you.…So this block of code is going to occur…whenever a touch happens,…and it's not on a particular object.…So, here, we can tell the text field…to resign first responder,…and dismiss itself.…
Let's see how that works in action.…
Released
10/6/2018- Creating an Xcode project
- Navigating the Xcode interface
- How model-view-controller works in an iOS app
- Troubleshooting UI-to-code connections
- Planning an app within your constraints
- Setting up the UI
- Testing your app on a device
Share this video
Embed this video
Video: Cancel keyboard entry