34

We're trying to figure out how to get the keyboard to hide, but we're having problems getting the textFieldShouldReturn to fire. Why?

This is what has been done:

*.h

@interface MultiSalesViewController : UIViewController <UITextFieldDelegate>

*.c

txtCardNumber.delegate = self;

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

Also, the textField has its delegate set to Files Owner in Interface Builder. One odd thing, is that the viewController's - (void)textFieldDidEndEditing:(UITextField *)textField is working.

How to get the hiding of the keyboard working?

Cœur
  • 37,241
  • 25
  • 195
  • 267
iPhone Developer
  • 519
  • 3
  • 6
  • 10

10 Answers10

81

I had the exact same issue and it was because I forgot to set the delegate for the text field in interface builder to 'files owner'.

Warren Crowther
  • 811
  • 5
  • 3
28

I had the same problem and, as Warren Crowther suggested, I managed to solve it by holding down CTRL and dragging from the TextBox to the "File's Owner" label.

(Gosh, I miss Visual Studio sometimes...!!)

enter image description here

(Apologies for repeating what's already been said, but I thought a screenshot might be useful !)

Mike Gledhill
  • 27,846
  • 7
  • 149
  • 159
12

I had the delegate set and everything. But I was using a UITextView instead of UITextfield...

Perhaps this will help someone trying to figure why delegate methods are not fired.

momo
  • 3,404
  • 6
  • 37
  • 66
10

I see you put it in your code, but for future visitors, add this to your code:

yourTextField.delegate = self;

sridvijay
  • 1,526
  • 18
  • 39
6

I think you are using xib. If so You also need to set delegate over there. Do Right Click on your UITextfiled in xib and you will have delegate option drag it to your file owner.

MANiK
  • 61
  • 1
  • 1
3

following is answer from Mike Gledhill and Warren Crowther updated with xcode 5 screenshot.

(to set UITextField delegate, press and hold ctrl + drag from the UITextField to the "File's Owner" yellow button, shown in image below. if UITextField delegate not set, textFieldShouldReturn method never gets called).

enter image description here

tmr
  • 1,500
  • 15
  • 22
3

be sure that your MultiSalesViewController implements the UITextFieldDelegate protocol:

@interface MultiSalesViewController : UIViewController <UITextFieldDelegate>

try adding [self becomeFirstResponder]; after [textField resignFirstResponder];


edit: just another thought.. does your UITextField have a value set for returnKeyType?

txtCardNumber.returnKeyType = UIReturnKeyDone;

i'm not sure if this has to be set for the function to work

james
  • 26,141
  • 19
  • 95
  • 113
  • 2
    Right now the textFieldShouldReturn callback is NOT firing at all despite the view controller implementing UITextFieldDelegate. This is odd because the textFieldDidEndEditing callback is. This seems odd because both are defined as part of the UITextFieldDelegate. So, my question is why is one firing and not the other? – iPhone Developer Jan 07 '11 at 15:43
2

I had everything wired up just right and - (BOOL)textFieldShouldReturn:(UITextField *)textField was still not being called!

As a work around I configured a method to fire on 'EditingDidEnd':

enter image description here

kraftydevil
  • 5,144
  • 6
  • 43
  • 65
1

Go to Connection Inspector and connect delegate to view controller .thats it .

Biren
  • 23
  • 1
  • 5
0

Checklist to get it to work:

  • Did you set your controller as delegate to UITextField Instance?

  • Make sure controller is not being deallocated by either assigning to property (Autorelease) or explicit retaining it.

San
  • 1,796
  • 13
  • 22