UiTextField和resignFirstResponder [英] UiTextField and resignFirstResponder

查看:109
本文介绍了UiTextField和resignFirstResponder的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的设置很奇怪.

我有一个名为 View1 View和一个ViewController名为 viewController1

I have a View called View1 and a ViewController call viewController1

View1 ViewController1 的子代.

View1 中,我使用代码UITextField创建并添加为子视图.

Inside View1, I created using code a UITextField and added as a subview.

在我的Viewcontroller中,我有viewController1 : UIViewController <UITextFieldDelegate>

现在..我想在 View1 中的文本字段得到键盘中的完成"按钮时退出键盘,所以我有

Now.. I would like to resign the keyboard when the text field within View1 gets the "Done" button in the keyboard so I have

- (BOOL)textFieldShouldReturn:(UITextField *)TEXTFIELD {
    [TEXTFIELD resignFirstResponder];
}

现在的问题是,如何在TEXTFIELD(在Viewcontroller中定义)和View中定义的文本字段之间建立连接?

Now the question is, how do I make the connection between TEXTFIELD (defined in my viewcontroller) and the textfield defined in my View?

我必须在interfaceBuilder中做什么吗?

Do I have to do something in interfaceBuilder?

也许我完全不在这里......

maybe I am totally off here....

一些提示非常感谢

推荐答案

Kendall是正确的.您需要将viewController1设置为文本字段的委托.

Kendall is right. You need to set your viewController1 as the delegate of your text field.

在上面的评论中,我认为您误会了textFieldShouldReturn方法. UITextField *作为参数传递给此方法,以便委托可以查看要返回的字段,并决定是否允许它返回.在您的情况下,您只有一个文本字段,因此只需辞职第一响应者并返回YES(我的代码在下面).

In your comment above, I think you're misunderstanding the textFieldShouldReturn method. The UITextField* is passed in as a parameter to this method, so that the delegate can see which field wants to return, and decide whether to allow it to. In your case, you only have one textfield, so just resign first responder and return YES (my code is below).

尝试在此textFieldShouldReturn函数上设置一个断点.它被叫吗?

Try putting a breakpoint on this textFieldShouldReturn function. Is it getting called?

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

这篇关于UiTextField和resignFirstResponder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆