How to hide the keyboard when done is pressed on a TextField

After lots of searching I finally found the answer.

When Done (Or Search, Yahoo, etc) is pressed on the popup keyboard on the iPhone, you have to send the textfield a resignFirstResponder message.

To know when done is pressed you need to make your controller the delegate for that textField. To do this, in interface builder, control click the textfield and drag to the controller and select delegate.

Once the controller is the delegate for the textField, you'll be able to implement the UITextFieldDelegate Protocol.

In this protocol is this message in particular:


- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
	[textField resignFirstResponder];
	return YES;
}

As you can see my controller is for multiple text fields and the message is handled by textFieldShouldReturn.

And to quote another source that really made this clear for me, here is an excerpt I found that helped.


The final observer pattern Cocoa makes easy are delegates. In a broad sense delegates are intended to handle more than simply "observations" but they don't always need to do more.
For example, all of the notifications that NSApplication and NSWindow are also sent to their delegate and could be handled there instead. Some classes send notification-like message to their delegates that are never sent as notifications. For example NSMenu, sends menuWillOpen: to its delegate but does not send an equivalent NSNotification.
To connect a delegate, just invoke:

[object setDelegate:delegateObject];

on an object which supports a delegate. The delegateObject can then receive any of the delegate messages it wants.

The article quoted above is available at Five Approaches to Listening, Observing and Notifying in Cocoa

Xcode iPhone MacOSX