保存UITextField测试的最佳方法:textFieldShouldReturn或textFieldDidEndEditing [英] Best method to save UITextField test: textFieldShouldReturn or textFieldDidEndEditing

查看:626
本文介绍了保存UITextField测试的最佳方法:textFieldShouldReturn或textFieldDidEndEditing的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标只是在用户点击键盘上的完成按钮后在UITextField上保存文本。我可以在extFieldShouldReturn或textFieldDidEndEditing中执行此操作:它有什么不同吗?还是有更好的方法?

my objective simply to save text on UITextField after user click done button on keyboard. I could either do this in extFieldShouldReturn or textFieldDidEndEditing: does it make any difference ? or there a better approach ?

谢谢!!

推荐答案

textFieldShouldReturn仅在用户按下返回键时才会调用。如果键盘由于某些其他原因(例如用户选择其他字段或将视图切换到另一个屏幕)而被解除,则不会是textFieldDidEndEditing,而是

textFieldShouldReturn is only called if the user presses the return key. If the keyboard is dismissed due to some other reason such as the user selecting another field, or switching views to another screen, it won't be but textFieldDidEndEditing will be.

最好的方法是使用textFieldShouldReturn来辞职响应者(隐藏键盘),如下所示:

The best approach is to use textFieldShouldReturn to resign the responder (hide the keyboard) like this:

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    //hide the keyboard
    [textField resignFirstResponder];

    //return NO or YES, it doesn't matter
    return YES;
}

当键盘关闭时,将调用textFieldDidEndEditing。然后,您可以使用textFieldDidEndEditing对文本执行某些操作:

When the keyboard closes, textFieldDidEndEditing will be called. You can then use textFieldDidEndEditing to do something with the text:

- (BOOL)textFieldDidEndEditing:(UITextField *)textField
{
    //do something with the text
}

但如果你实际上只有当用户明确按下键盘上的go或send或search(或其他)按钮时才会执行操作,那么你应该将该处理程序放在textFieldShouldReturn方法中,如下所示:

But if you actually want to perform the action only when the user explicitly presses the "go" or "send" or "search" (or whatever) button on the keyboard, then you should put that handler in the textFieldShouldReturn method instead, like this:

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    //hide the keyboard
    [textField resignFirstResponder];

    //submit my form
    [self submitFormActionOrWhatever];

    //return NO or YES, it doesn't matter
    return YES;
}

这篇关于保存UITextField测试的最佳方法:textFieldShouldReturn或textFieldDidEndEditing的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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