如何让未知的 UITextField 放弃第一响应者?

发布于 2024-11-05 08:03:30 字数 380 浏览 5 评论 0原文

我的视图有两个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

神仙妹妹 2024-11-12 08:03:30

一种可爱的方法可以做到这一点,而无需跟踪哪个字段处于活动状态:

// This causes the current responder (eg. an input field) to resignFirstResponder and
[self.endEditing:YES];

One lovely way of doing this without having to keep track of which field is active:

// This causes the current responder (eg. an input field) to resignFirstResponder and
[self.endEditing:YES];
暗恋未遂 2024-11-12 08:03:30

[self.view endEditing:YES] 替换为以下内容...

[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];

Replace [self.view endEditing:YES] with the below one...

[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
最笨的告白 2024-11-12 08:03:30

调用 uitextfielddelegate 方法正在进行编辑的文本字段。这样您就不必面临检测正在编辑哪个文本字段的问题。

因此,实现 uitextfielddelegate 方法并将文本字段的委托分配给您实现方法并处理其中的响应的类。

您应该感兴趣的方法是:

textFieldDidEndEditing:

告诉代理指定文本字段的编辑已停止。

- (void)textFieldDidEndEditing:(UITextField *)textField

参数

文本字段
编辑结束的文本字段。

讨论

在文本字段放弃其第一响应者状态后调用此方法。您可以使用此方法来更新委托的状态信息。例如,您可以使用此方法来隐藏仅在编辑时才可见的覆盖视图。
委托实现此方法是可选的。

可用性

适用于 iOS 2.0 及更高版本。

声明于

UITextField.h

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:

textFieldDidEndEditing:

Tells the delegate that editing stopped for the specified text field.

- (void)textFieldDidEndEditing:(UITextField *)textField

Parameters

textField
The text field for which editing ended.

Discussion

This method is called after the text field resigns its first responder status. You can use this method to update your delegate’s state information. For example, you might use this method to hide overlay views that should be visible only while editing.
Implementation of this method by the delegate is optional.

Availability

Available in iOS 2.0 and later.

Declared In

UITextField.h

森林迷了鹿 2024-11-12 08:03:30

您可以使用 textFieldDidBeginEditing 委托自行跟踪哪一个是当前的。

You may keep track yourself which one is the current one, by using the textFieldDidBeginEditing delegate.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文