tags:

views:

3747

answers:

2

I have a weird setup.

I have a View call 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 as subview.

In my Viewcontroller, I have viewController1 : UIViewController

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

+3  A: 

Connect the delegate property of the text field to your view controller in IB (should be File's Owner for that view). Also note in the view controller header that you conform to the protocol.

Or you can just set the texfield.delegate property to self in the viewDidLoad method of the view controller.

Kendall Helmstetter Gelner
I now have my View1 's delegate hooked up to the FileOwner within IBSTill doesnt work :(In the method (BOOL)textFieldShouldReturn:(UITextField *)returnTextField.The returnTextField should match the textfield in the view.I just dont see where I can make that connection.
It's the delegate, that's the only connection you can make. As noted, make sure the view controller header implements the UITextFieldDelegate protocol.
Kendall Helmstetter Gelner
+5  A: 

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;
}
Jane Sales
I can't for the life of me work out why this doesn't work for me.
CVertex