Dismissing the Keyboard

As you may have noticed, the keyboard doesn’t go away by itself when a text field loses focus. To get the keyboard to disappear, you have to add a little code to the program. What you need to do is change the View, which is the background of the app, so that it is able to respond to a tap. When the view intercepts a tap, it will then send a message to the text field to resign control. This makes the keyboard disappear. The first thing to do is set up the code to handle the event and then tie the event to the code.

In LMAViewController.h, add this line between the @interface line and the @end line to define a new action method:

- (IBAction)backgroundTap:(id)sender;

In LMAViewController.m, ...

Get Learning Mobile App Development: A Hands-on Guide to Building Apps with iOS and Android now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.