按下另一个按钮时执行当前的 UITextField 自动更正建议
我正在我的应用程序中实现聊天功能,与 iPhone 的内置消息应用程序非常相似。我在按钮旁边有一个 UITextField。用户在文本字段中输入内容,文本字段通常会建议各种自动更正。在内置的消息应用程序中,点击发送按钮将导致执行当前可见的自动更正建议。我正在我的应用程序中寻找这种行为,但没有找到任何东西。
有谁知道当激活完全独立的控件时以编程方式执行 UITextField 当前可见的自动更正/自动完成建议的方法?显然这在某种程度上是可能的。
I am implementing chat in my application, very similar to the iPhone's built-in Messages app. I have a UITextField next to a button. The user types something into the text field, and very often the text field suggests various autocorrections. In the built-in Messages app, tapping the Send button will cause the currently visible autocorrection suggestion to execute. I am seeking this behavior in my application, but haven't been able to find anything.
Does anyone know of a way to programmatically execute the currently visible autocorrection/autocomplete suggestion of a UITextField when a completely separate control is activated? It's obviously possible somehow.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在字段上调用
-resignFirstResponder
。这迫使它接受自动更正。如果您不想关闭键盘,可以立即再次调用-becomeFirstResponder
。Call
-resignFirstResponder
on the field. That forces it to accept the autocorrect. If you don't want to dismiss the keyboard, you can immediately follow that with a call to-becomeFirstResponder
again.对于 esilver:您可以通过让不同的文本字段成为FirstResponder,然后让相关的文本字段成为FirstResponder 来完成此操作,而无需放弃第一响应者。在这种情况下键盘不会移动,也不会触发任何隐藏通知。如果您没有任何其他文本字段,请创建一个虚拟文本字段并将其设置为隐藏 = YES。
For esilver: you can do this without resigning first responder by having a different textfield becomeFirstResponder and then having the relevant textfield becomeFirstResponder. The keyboard will not move in this case, and not trigger any hide notifications. If you don't have any other textfields, create a dummy textfield and set it to hidden = YES.
由于辞职并重新担任第一响应者可能会产生副作用(大量通知、键盘显示/隐藏触发器等),因此我一直在寻找一种替代的、不那么残酷的方法。经过相当多的搜索后,我发现这就是您在
UITextView
(或UITextField
fwiw)中接受自动更正所需要做的全部事情:希望这会有所帮助;)
Since resigning and re-assuming first responder may have side effects (lots of notifications, keyboard show/hide triggers, etc), I've been looking for an alternative, less brutal way. After quite some search, I found this is all you need to do to accept autocorrections in a
UITextView
(orUITextField
fwiw):Hope this helps ;)
这段代码解决了我的问题。
类别代码。
This code was solve problem in my case.
Category code.
在 Nick Locking 的建议的基础上,我们编写了一个类别方法,用于处理任何待处理的自动更正建议,而无需关闭键盘(并且不会触发 will/did 隐藏/显示通知)。
Building on Nick Locking's suggestions, here is a category method we wrote to process any pending autocorrect suggestions without dismissing the keyboard (and without triggering the will/did hide/show notifications).
这是所讨论的解决方案的快速清晰回顾...
创建一个虚拟文本视图来制作响应者,然后返回到原始文本视图。确保它是第一响应者。
设置:
方法:
Here's a quick clean recap of solutions discussed...
Create a dummy textview to make responder and then return to original textview. make sure it was first responder.
setup:
Method: