在 iOS 中未调用 textFieldShouldReturn

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4626320/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:27:53  来源:igfitidea点击:

textFieldShouldReturn not being called in iOS

iosiphoneuitextfield

提问by iPhone Developer

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

我们试图弄清楚如何让键盘隐藏,但我们在textFieldShouldReturn触发时遇到了问题。为什么?

This is what has been done:

这是已经完成的事情:

*.h

*。H

@interface MultiSalesViewController : UIViewController <UITextFieldDelegate>

*.c

*。C

txtCardNumber.delegate = self;

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

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

此外,textField在 Interface Builder中将其委托设置为 Files Owner。一件奇怪的事情是 viewController- (void)textFieldDidEndEditing:(UITextField *)textField正在工作。

How to get the hiding of the keyboard working?

如何让键盘隐藏工作?

回答by Warren Crowther

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

我遇到了完全相同的问题,这是因为我忘记将界面构建器中文本字段的委托设置为“文件所有者”。

回答by Mike Gledhill

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.

我遇到了同样的问题,正如 Warren Crowther 建议的那样,我设法通过按住 CTRL 并从文本框拖动到“文件所有者”标签来解决它。

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

(天哪,我有时会想念 Visual Studio ......!!)

enter image description here

在此处输入图片说明

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

(抱歉重复已经说过的内容,但我认为截图可能有用!)

回答by momo

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

我已经设置了代表和一切。但我使用的是 UITextView 而不是 UITextfield ......

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

也许这将有助于试图弄清楚为什么不触发委托方法的人。

回答by sridvijay

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

我看到你把它放在你的代码中,但对于未来的访问者,将它添加到你的代码中:

yourTextField.delegate = self;

yourTextField.delegate = self;

回答by MANiK

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.

我认为您正在使用xib。如果是这样,您还需要在那里设置委托。在 xib 中右键单击您的 UITextfiled,您将有委托选项将其拖动到您的文件所有者。

回答by tmr

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

以下是 Mike Gledhill 和 Warren Crowther 的回答,更新了 xcode 5 屏幕截图。

(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).

(要设置 UITextField 委托,按住 ctrl + 从 UITextField 拖动到“文件所有者”黄色按钮,如下图所示。如果未设置 UITextField 委托,则永远不会调用 textFieldShouldReturn 方法)。

enter image description here

在此处输入图片说明

回答by kraftydevil

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

我把所有的东西都连接好,- (BOOL)textFieldShouldReturn:(UITextField *)textField但仍然没有被调用!

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

作为解决方法,我配置了一个方法来触发“EditingDidEnd”:

enter image description here

在此处输入图片说明

回答by Biren

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

转到连接检查器并将委托连接到视图控制器。就是这样。

回答by james

be sure that your MultiSalesViewControllerimplements the UITextFieldDelegateprotocol:

确保您MultiSalesViewController实现了UITextFieldDelegate协议:

@interface MultiSalesViewController : UIViewController <UITextFieldDelegate>

@interface MultiSalesViewController : UIViewController <UITextFieldDelegate>

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

尝试添加[self becomeFirstResponder];[textField resignFirstResponder];



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

编辑:只是另一个想法..你UITextField有一个值returnKeyType吗?

txtCardNumber.returnKeyType = UIReturnKeyDone;

txtCardNumber.returnKeyType = UIReturnKeyDone;

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

我不确定是否必须设置此功能才能正常工作

回答by San

Checklist to get it to work:

清单得到它的工作:

  • Did you set your controller as delegateto UITextField Instance?

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

  • 您是否将控制器设置为 UITextField 实例的委托

  • 通过分配给属性(自动释放)或显式保留它,确保控制器没有被释放。