I’m aware that I said that my next post would be about reading large XML files in .Net, but this has come up and I wanted to write it down somewhere to make sure I didn’t forget it…!
I’m working on an iPhone application at the moment. I’m learning as I go, so progress is quite slow. Recently I’ve been trying to work out how to dismiss the on-screen keyboard when the user has finished editing the contents of a text box. Due in part to the (Now lifted) Non-Disclosure Agreement that Apple was forcing people to accept if they wanted to develop for the iPhone, there’s not a lot of information out there on iPhone development.
In any case, I recently stumbled across the answer here, which seems to work quite nicely. I’ve run up a quick sample to illustrate it, as much for my own benefit the next time I need to do it as for anything else.
Essentially, you need to do a few things:
- The Controller of the View that contains the text field(s) you’re interested in needs to implement the UITextFieldDelegate protocol.
- The text fields themselves need to have their delegate set to the View Controller.
- You need to implement a function that will be called every time the user hits return in a text field that’s within your View.
To satisfy point 1, your View Controller’s .h file needs to contain something like this:
@interface MyViewController : UIViewController <UITextFieldDelegate> {
For point 2, it’s a case of firing up Interface Builder and, for each of the text fields you’re interested in, dragging the “delegate” connector onto the “File’s Owner”. When you’ve got it right, the connector tab for the text field will look like this:
![]()
For point 3, you need to implement the function below with your View Controller’s .m file:
- (BOOL)textFieldShouldReturn: (UITextField *)theTextField {
[theTextField resignFirstResponder];
return YES;
}
…and that should do you.