textFieldShouldReturn not being called in iOS

2020-05-21 08:25发布

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?

10条回答
孤傲高冷的网名
2楼-- · 2020-05-21 08:38

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

查看更多
Rolldiameter
3楼-- · 2020-05-21 08:39

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

查看更多
别忘想泡老子
4楼-- · 2020-05-21 08:39

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

查看更多
时光不老,我们不散
5楼-- · 2020-05-21 08:40

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

查看更多
淡お忘
6楼-- · 2020-05-21 08:42

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.

查看更多
唯我独甜
7楼-- · 2020-05-21 08:46

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

查看更多
登录 后发表回答