UITextField 自动完成?

发布于 2024-09-08 09:39:54 字数 191 浏览 6 评论 0原文

我试图弄清楚是否有一种方法可以在 UITextField 中针对特定值实现自动完成功能。

我知道 UITextField 可以使用 iPhone 字典来完成此操作(很像在 safari 中搜索 google 等),但我希望能够以编程方式将其正确设置为我指定的某些值。

如何做到这一点?

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

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.

How to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

晨敛清荷 2024-09-15 09:39:54

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

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

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

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

最后,该解决方案非常适合我们的需求。

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;

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.

屋顶上的小猫咪 2024-09-15 09:39:54

或者,您可以使用此 UITextField 子类(受 DOAutocompleteTextField 启发):

https://github.com/hoteltonight/HTAutocompleteTextField

它还有更多功能并且正在积极开发中。该示例向您展示如何使用数组作为自动建议文本的数据源。它采用与 DOAutocompleteTextField 相同的方法,因为它在用户键入时在文本字段中显示“幻影”的建议完成文本。

Alternatively, you can use this UITextField subclass (inspired by DOAutocompleteTextField):

https://github.com/hoteltonight/HTAutocompleteTextField

It's got a few more features and is actively developed. The example shows you how to use an array as the data source for the autosuggest text. It takes the same approach as DOAutocompleteTextField, in that it shows the suggested completion text "ghosted" in the text field as the user types.

场罚期间 2024-09-15 09:39:54

你研究过UISearchDisplayController吗? Stack Overflow 上有一些线程,其中包括核心数据参考(如果您正在使用的话)。还有其他一些替代方法

Have you looked into UISearchDisplayController? There are a few threads here on Stack Overflow, including Core Data references if that is what you are using. Also some alternative methods, elsewhere.

多彩岁月 2024-09-15 09:39:54

在前面提到的 Ray Wenderlich 教程的帮助下,我刚刚实现了一个版本来过滤现有 UITableView 中的名称。

我将文本字段的委托设置为视图控制器,将视图控制器设置为 UITextFieldDelegate 并实现了这两个方法:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSString *substring = [NSString stringWithString:textField.text];
    substring = [substring stringByReplacingCharactersInRange:range withString:string];
    [self searchAutocompleteEntriesWithSubstring:substring];
    return YES;
}

- (void)searchAutocompleteEntriesWithSubstring:(NSString *)substring
{   
    NSMutableArray *autoCompleteArray = [[NSMutableArray alloc]init];
    [self retrieveData];

    for(NSString *curString in _staffTableArray)
    {
        NSString *lowerCaseCur = [curString lowercaseString];
        NSRange substringRange = [lowerCaseCur rangeOfString:substring];
        if (substringRange.location == 0)
        {
            [autoCompleteArray addObject:curString];
        }
    }
    if (![substring isEqualToString:@""])
    {
         _staffTableArray = [NSMutableArray arrayWithArray:autoCompleteArray];
    }  
    [_staffListTableView reloadData];
}

With the help of the aforementioned Ray Wenderlich tutorial, I just implemented a version of this to filter names in an existing UITableView.

I set my text field's delegate as my view controller, my view controller as a UITextFieldDelegate and implemented these two methods:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSString *substring = [NSString stringWithString:textField.text];
    substring = [substring stringByReplacingCharactersInRange:range withString:string];
    [self searchAutocompleteEntriesWithSubstring:substring];
    return YES;
}

- (void)searchAutocompleteEntriesWithSubstring:(NSString *)substring
{   
    NSMutableArray *autoCompleteArray = [[NSMutableArray alloc]init];
    [self retrieveData];

    for(NSString *curString in _staffTableArray)
    {
        NSString *lowerCaseCur = [curString lowercaseString];
        NSRange substringRange = [lowerCaseCur rangeOfString:substring];
        if (substringRange.location == 0)
        {
            [autoCompleteArray addObject:curString];
        }
    }
    if (![substring isEqualToString:@""])
    {
         _staffTableArray = [NSMutableArray arrayWithArray:autoCompleteArray];
    }  
    [_staffListTableView reloadData];
}
梦在深巷 2024-09-15 09:39:54

使用这个委托方法。您可以替换您指定的值。

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;   // return NO to not change text
      if ([string isEqualToString:@"StackO"]) {
           textField.text=@"StackOverflow";
      }
    return YES;
}

use this delegate method. you can replace values that you specify.

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;   // return NO to not change text
      if ([string isEqualToString:@"StackO"]) {
           textField.text=@"StackOverflow";
      }
    return YES;
}
烂人 2024-09-15 09:39:54

刚刚面对这个线程,因为我需要类似的东西。如何使用 UITextfieldDelegate 的方法实现您自己的搜索:

- (BOOL)textField:(UITextField *) textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

您可能知道,每个 UITextfield 的输入都会调用此方法。

Just faced with this thread because I need something similar. How about implementing you own search with the UITextfieldDelegate's method:

- (BOOL)textField:(UITextField *) textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

As you'd probably know this method is called for every UITextfield's typing.

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