如何让未知的 UITextField 放弃第一响应者?
我的视图有两个 UITextFields
和一个 UISwitch
。如果用户编辑textField
,然后立即触摸开关(不按回车键),则文本将保留为他们键入的内容,而不会自动更正
。
如果我知道他们正在输入哪个 textField
,我可以通过调用 [textField resignFirstResponder]
强制自动更正完成。但用户可能会在任一 textField
中键入内容,因此我不知道要调用哪一个。
我该如何解决这个问题?有没有办法检测正在使用哪个textField
?还是我没有想到的更简单的事情?
My view has two UITextFields
and a UISwitch
. If a user is edits a textField
, and then immediately touches the switch (without pressing return), the text is left as they typed it, without AutoCorrect
.
If I know which textField
they were typing in, I can force the autocorrect to complete by calling [textField resignFirstResponder]
. But the user could be typing in either textField
, so I don't know which one to call.
How can I get around this? Is there a way of detecting which textField
was being used? Or something simpler I haven't thought of?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
一种可爱的方法可以做到这一点,而无需跟踪哪个字段处于活动状态:
One lovely way of doing this without having to keep track of which field is active:
将
[self.view endEditing:YES]
替换为以下内容...Replace
[self.view endEditing:YES]
with the below one...调用 uitextfielddelegate 方法正在进行编辑的文本字段。这样您就不必面临检测正在编辑哪个文本字段的问题。
因此,实现 uitextfielddelegate 方法并将文本字段的委托分配给您实现方法并处理其中的响应的类。
您应该感兴趣的方法是:
The uitextfielddelegate methods are called for the textfield on which the editing is in progress. So that way you needn't be facing the problem of detecting which text field is being edited.
So implement the uitextfielddelegate methods and assign the delegate of the text field to the class where you implement the methods and handle the responses in them.
The methods which you should be interested in are:
您可以使用 textFieldDidBeginEditing 委托自行跟踪哪一个是当前的。
You may keep track yourself which one is the current one, by using the textFieldDidBeginEditing delegate.