UI TextField Delegate方法textFieldShouldReturn:的返回值是什么意思? [英] What does the return value mean for the UI TextField Delegate method textFieldShouldReturn:?
问题描述
苹果文档提供:
如果文本字段应处理返回
的按钮,请委托人。-(BOOL)textFieldShouldReturn:(UITextField *)textField
参数
textField
按下返回按钮的文本字段。
返回值
是
如果文本字段应为返回按钮实现其默认的
行为;否则,否
。
讨论用户每次点击
返回按钮时,文本字段都会调用此方法。点击按钮时,可以使用此方法实现任何自定义
行为。
我的问题是返回值是什么值吗?我已经在此方法中实现了行为,因此返回的结果没有区别。这不是执行操作的正确方法吗?
例如,如果我实现了搜索功能,是否应该以这种方法或其他方式触发搜索动作。
这是在用户点击Return键盘键(无论它是否贴有标签)时触发操作的正确方法
textFieldShouldReturn:
委托方法的返回值几乎没有关系。如果您要处理的是单个文本字段,那绝对没关系。
前一段时间我碰到一个问题,使我意识到在正确的情况下,返回值确实很重要。我的屏幕上有几个文本字段,然后有一个文本视图。我使用此文本字段委托方法将第一响应者从文本字段更改为文本字段,再更改为文本视图。我发现,如果我在此委托方法中返回 YES
,然后将文本视图设为第一响应者,则换行符将被发送到文本视图。
NO
。 The apple docs offer:
Asks the delegate if the text field should process the pressing of the return button.
- (BOOL)textFieldShouldReturn:(UITextField *)textField
Parameters
textField
The text field whose return button was pressed.Return Value
YES
if the text field should implement its default behavior for the return button; otherwise,NO
.Discussion The text field callsthis method whenever the user tapsthe return button. You can use this method to implement any custom behavior when the button is tapped.
My question is what does the return value do? I have been implementing the behavior in this method so it makes no difference what is returned. Is this not the correct method to perform the action?
For instance, if I implement a search function, should I trigger the search action in this method or somewhere else.
This is the correct method to trigger an action when the user taps the Return keyboard key (whatever it happens to be labeled).
The return value from the textFieldShouldReturn:
delegate method almost never matters. If you are dealing with a single text field then it definitely doesn't matter.
I ran into one issue a while back that made me realize that just under the right situation, the return value does matter. I had a screen with several text fields and then a text view. I was using this text field delegate method to change the first responder from text field to text field to text view. I found that if I returned YES
in this delegate method and then made the text view the first responder, the newline was being sent to the text view.
As a result of this, I now always return NO
from this delegate method to be safe.
这篇关于UI TextField Delegate方法textFieldShouldReturn:的返回值是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!