View Full Version : Closing the keyboard

Oct 30, 2008, 02:33 PM
How do I close the keyboard that pops up when a textfield is selected? I don't want to do anything special, I just want to recognize that the done button has been pressed and close the keyboard. Optionally and as a better alternative, being able to use my own button to close it. There doesn't seem to be any UIKeyboard class or anything like that, I've already gone through UITextField, UIView, UIWindow, UIControl, UIResponder, etc. and I can't find anything.
Thanks, Nate

Oct 30, 2008, 02:44 PM
Have you worked with delegates before?

Delegates are pretty much "what happens when I do this?"

So the UITextField class has all the "what is it and what does it look like."

The UITextFieldDelegate contains your answer: "textFieldShouldReturn:"

Set the textField's delegate to self. Then implement the delegate by adding it in the header file of the same class.

Now use the "textFieldShouldReturn:" method and use the [textField resignFirstResponder];.

Don't forget to return YES.

Oct 30, 2008, 02:47 PM
No... I can't find a tutorial or example that I can understand, it's a work in progress. But thanks, this should be enough.

Oct 30, 2008, 02:51 PM
No problem. I had no idea what delegates were when starting out (came from Java and never used a delegate there).

Turns out delegates are very, very useful.

You get to control stuff you didn't think you'd be able to, like for scrollviews, you can control when the person started scrolling,when they lift their finger, and even when it starts to deaccelerate. Textfields you even get to control what happens everytime they type a character.

Oct 30, 2008, 02:58 PM
Could you possibly point me at whatever docs/tutorials you have handy for delegates? I figure if I get enough on the same topic, I'll learn them one way or another :rolleyes:

Oct 31, 2008, 10:45 AM
[textField resignFirstResponder];


Oct 31, 2008, 10:55 AM
Thank you you're a life saver! However, I still have no idea how to use delegates and google isn't being nice to me, nor is apple's docs. Any help there would be appreciated.
Thanks a million,

Oct 31, 2008, 11:27 AM
Essentially, (for the built-in protocols at least) you declare your class to be a delegate of something (list here, under "Protocol References": https://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIKit_Framework/index.html)

For example, making your class a UIWebViewDelegate from the header:

@interface WebViewController : UIViewController <UIWebViewDelegate>

Then in your implementation you set the UI component you want the protocol to work with, which in this example could be:

webView.delegate = self;

(Where webView is a UIWebView.)

Then, when certain things happen within the UIWebView, for instance a page starting or finishing loading, methods in your implementation class will be fired. The list of methods can be found in each delegate's protocol reference (listed above), so for instance UIWebViewDelegate is here: (https://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIWebViewDelegate_Protocol/Reference/Reference.html)

And there you can see:


The names are fairly self-explanatory, and the docs are pretty good too :)

Proper explanation from the Cocoa docs here: http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/CocoaFundamentals/CommunicatingWithObjects/chapter_6_section_4.html

Oct 31, 2008, 12:51 PM
Thank you so much... that helped tons!