If you followed my last tutorial, posted here, you may have noticed that you could not dismiss the keyboard once you had selected the text field.

Today, i will be showing you how to do this.

If you need the .h or .m files, you can download them here.

To get started, open up the HelloYou app we created in the last tutorial. Once in it, navigate to the .h.



Once we are in the .h we are going to delete the two IBOutlets, leaving the UILabel and UITextField functions. So your .h should now look like this:



Then we're going to add this:
Code:
@property (nonatomic, retain) IBOutlet UILabel *hello ;
@property (nonatomic, retain) IBOutlet UITextField *name ;
The @property is a directive, it tells Xcode to generate the getters and setters for this member variable. The nonatomic and retain parameters basically tell Xcode how to treat the property. The IBOutlets are the same as when we inserted them in the last tutorial. Read up on them online, because you're going to be using them a lot.

Now your .h should look like this:



Next we're going to look at setting the action, in this case dismissing the keyboard.

Start by adding this to your .h, right below the first IBAction:
Code:
-(IBAction) dismissKeyboard ;
This is basically just creating the name of the action we are creating.
Your .h should look like this:



Now that we have created the action, we must set the parameters for what it does in the .m. So open up your .m and add this code right below the @implementation
Code:
@synthesize name, hello ;
@synthesize relates to the @property we added to the .h earlier. @synthesize tells Xcode to create the setter and/or getter if you do not include them in the @implementation. Basically, they make it a lot easier for you, coding wise.

Your .m should look like this now. Pay attention to the highlighted text, it's what you just added.



Next, we will define the dismissKeyboard IBAction we specified in the .h before. Start by adding this just below where you placed the NSString in the last tutorial:
Code:
-void) textFieldShouldReturn: (id) sender
{
	[sender resignFirstResponder] ;
-(IBAction) dismissKeyboard
{
	[name resignFirstResponder] ;
}
Your .m should now look like this:



Again, pay attention the highlighted part. It's the code you just added in.

textFieldShouldReturn sets it so that the keyboard will move offscreen when we tap the Return button on the keyboard, and dismissKeyboard sets it so that the keyboard will move offscreen when we tap anywhere outside of the keyboard.

Next we are going to go to our .storyboard, and set all of the delegates.

First we must select the view itself and change it to a class. We can do this by going to the column at the right and switching to the Identity Inspector, like I've done below.



The Identity Inspector is the third option from the right; on mine it's highlighted. Look at the above image if you can't find it.

Now you want to go to the first section, the Custom Class one, and change the Class value to UIControl.

Next we right click the right click on the View Controller, drag dismissKeyboard to a blank space on the iDevice, and set the action as Touch Up Inside.





This will allow us to tap anywhere on our iDevice screen to dismiss the keyboard.

Next we select the text field, like shown below:



and open the Connections Inspector, the last arrow on the right column.



Under the Outlets subheading, the second one, you'll see delegate. Drag it it to the ViewController, the left box rght below the iDevice view.

Drag this:


Here:


And you should see this:


That's it, we're done. Now you can click Run and see if it works correctly.

As always, any questions, comments, or concerns, post them below. Thanks for reading.