Friday, April 20, 2012

Number 1: Dismissing The Keyboard after it appears

How To Dismiss the keyboard after it appears from clicking on a UITextField or UITextArea


Dismissing the keyboard was a central point of frustration for me once I began learning XCode and the Objective-C language.  The problem was that it is so easy to see the keyboard appear with that first HelloWorld, but infinitely frustrating because dismissing it takes a tab bit more knowledge.
The solution to dismiss the keyboard is in the following code:  resignTheFirstResponder.  Of course, I did not know what a firstResponder was and I certainly did not know how to resign it.  But, there are plenty of tutorials that simply said put this code block in the implemention file (the .m file), and magically the keyboard would disappear.  And sure enough it did, until I started trying to make my own UIViewControllers.
What will make your life easier is first putting in the delegate between the greater than less than signs <UITextFieldDelegate, UITextAreaDelegate> in the .h header file.  What this does is to inform XCode that you will want to use some of their code so that when you begin typing it will automatically suggest the code you with the correct syntax allowing you to avoide misspelling words.
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return NO;
}

I was very happy when this worked.  However, I remain frustrated that when I add this delegate information to a new viewController’s .h and .m files that the keyboard does not go away.  I can only guess that I do not have my connection right.  Perhaps someone, can outline the steps in the next section to create and connect a new viewcontroller to its class and IBOutlets so that the code to dismiss the keyboard is recognized.  At the time of  this writing, this is the number one issue that frustrated me as a new developer.  Not being able to dismiss the keyboard when using Storyboards, or when creating my own ViewController class.

No comments:

Post a Comment