UITextField 自动完成? [英] UITextField Autocomplete?

查看:25
本文介绍了UITextField 自动完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想弄清楚是否有办法在 UITextField 中为特定值实现自动完成功能.

I am trying to figure out if there is a way to implement an autocomplete functionality in a UITextField for specific values.

我知道 UITextField 可以使用 iPhone 字典来做到这一点(很像在 safari 中搜索 google 等),但我希望能够以编程方式使其正确到我指定的某些值.

I know that the UITextField can do this using the iPhone dictionary (much like searching google in safari, etc), but I want to be able to programmatically have it correct to certain values that I specify.

如何做到这一点?

推荐答案

Brett,

我在最近的一个相当大的项目中做了一些与此非常相似的事情.我们有一个不断变化的自动完成术语列表,并围绕它们构建了一个自动完成.

I did something very similar to this while working on a recent and rather large project. We had a constantly changing list of auto complete terms and built an auto-complete around them.

首先,您需要制作某种类型的自动完成控制器.它应该接受一个字符串并返回该字符串的所有可能的自动完成术语.

First, you'll want to make some type of auto-complete controller. It should take a string and return all possible auto complete terms for that string.

-(NSArray *)completionsForString:(NSString *)myString;

然后,查看 UIMenuController 类.它是在许多应用程序中显示剪切/复制/粘贴选项的类.您可以获取它的共享实例,自己填充菜单项,并将其显示在文本字段上方.然后,用户只需点按他们想要的术语即可.

Then, check out the UIMenuController class. It's the class that shows the cut/copy/paste options in many applications. You can get the shared instance of it, populate the menu items yourself, and show it above the text field. The user can then simply tap the term they want.

最终,该解决方案非常适合我们的需求.我希望它对你有用.

In the end, the solution worked really well for our needs. I hope it works for you.

这篇关于UITextField 自动完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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