iPhone Developer
iPhone Developer

Reputation: 519

textFieldShouldReturn not being called in iOS

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?

Upvotes: 34

Views: 36253

Answers (10)

San
San

Reputation: 1796

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.

Upvotes: 0

kraftydevil
kraftydevil

Reputation: 5246

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

Upvotes: 2

Biren
Biren

Reputation: 23

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

Upvotes: 1

tmr
tmr

Reputation: 1530

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

Upvotes: 3

momo
momo

Reputation: 3474

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.

Upvotes: 12

sridvijay
sridvijay

Reputation: 1526

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

yourTextField.delegate = self;

Upvotes: 10

Mike Gledhill
Mike Gledhill

Reputation: 29161

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 !)

Upvotes: 28

MANiK
MANiK

Reputation: 61

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.

Upvotes: 6

Warren Crowther
Warren Crowther

Reputation: 811

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'.

Upvotes: 81

james
james

Reputation: 26271

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

Upvotes: 3

Related Questions