UiTextField and resignFirstResponder

user82383 picture user82383 · Mar 26, 2009 · Viewed 38.4k times · Source

I have a weird setup.

I have a View called View1 and a ViewController call viewController1

in IB, View1 is a child of the ViewController1.

Inside View1, I created using code a UITextField and added as a subview.

In my Viewcontroller, I have viewController1 : UIViewController <UITextFieldDelegate>

Now.. I would like to resign the keyboard when the text field within View1 gets the "Done" button in the keyboard so I have

- (BOOL)textFieldShouldReturn:(UITextField *)TEXTFIELD {
    [TEXTFIELD resignFirstResponder];
}

Now the question is, how do I make the connection between TEXTFIELD (defined in my viewcontroller) and the textfield defined in my View?

Do I have to do something in interfaceBuilder?

maybe I am totally off here....

Some hints greatly appreciated

Answer

Jane Sales picture Jane Sales · Mar 26, 2009

Kendall is right. You need to set your viewController1 as the delegate of your text field.

In your comment above, I think you're misunderstanding the textFieldShouldReturn method. The UITextField* is passed in as a parameter to this method, so that the delegate can see which field wants to return, and decide whether to allow it to. In your case, you only have one textfield, so just resign first responder and return YES (my code is below).

Try putting a breakpoint on this textFieldShouldReturn function. Is it getting called?

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