简体   繁体   English

子视图上的UITextField返回键

[英]UITextField return key on subview

I've got a UIViewController with an additional small UIView I created on top of it (subview). 我有一个UIViewController ,还有一个在它上面创建的附加小UIView (子视图)。 When I click a button this view hovers to the center of the screen. 当我单击一个按钮时,该视图将悬停在屏幕中央。 The issue is the i've got a UITextField in the additional UIView and i cannot seem to get the return key to work. 问题是我在附加的UIView有一个UITextField ,而且我似乎无法使返回键正常工作。

Although I set my IBAction to the event "Editing did end" of the text field, when i click the return key, the IBAction doesn't run. 尽管我将IBAction设置为文本字段的事件“ Editing IBAction end”,但是当我单击返回键时, IBAction不会运行。

What am I doing wrong? 我究竟做错了什么?

确保接口上的UITextFieldDelegate

you just set Your Delegate for example :- yourtextfile.delegate=self; 您只需设置您的代表,例如: yourtextfile.delegate=self; and also dont forget to add delegate method in to .h file 并且也不要忘记在.h文件中添加委托方法

@interface contacts_detailView : UIViewController<UITextFieldDelegate>

and then you delegate textFieldDidEndEditing 然后委派textFieldDidEndEditing

- (void)textFieldDidEndEditing:(UITextField *)textField
{
//your new view apear code here 
    [textField resignFirstResponder];
}

Clicking on "Return" doesn't trigger an "Editing did end" event. 单击“返回”不会触发“编辑已结束”事件。 Only when you'll call resignFirstResponder on the UITextField will you see the event triggered. 仅当您将在UITextField上调用resignFirstResponder时,您才会看到触发的事件。

What you should do is set your UIViewController as the delegate of the UITextField and implement the method 您应该做的是将UIViewController设置为UITextField的委托并实现该方法

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

First of all, delegate the TextField to the file's owner like this: 首先,将TextField委托给文件的所有者,如下所示:

yourtextField.delegate = self in your viewDidLoad. yourtextField.delegate = self您的viewDidLoad中的yourtextField.delegate = self

And then add this to the view controller 然后将其添加到视图控制器

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

It should work. 它应该工作。

There is no need to write more code for key board return. 无需为键盘返回编写更多代码。 Please write this in your .m file , it will work for any number of text field , no need to write again again for different textfield. 请将此代码写入您的.m文件中,它将适用于任意数量的文本字段,而无需再次针对不同的文本字段进行写入。

use <UItextfieldDelegate> in your .h file. 在您的.h文件中使用<UItextfieldDelegate> Then make wiring with fileowner in nib file. 然后在nib文件中与fileowner进行接线。

- (BOOL)textFieldShouldReturn:(UITextField *)textField
    {
     [self.view endEditing:YES];
     return YES;
     }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM