iOS:当我开始在 UITextfield 中输入时如何调用 API 运行时?

iOS : How to call an API runtime when I start typing in UITextfield?

这是一个例子: 图片 1 - 它包含一个 UITextfield,例如 emailTextField。当我开始输入时,然后根据其在后台的关键字 API 将调用,它将根据您的关键字给出响应。

API 用于搜索就像

url/collaborator/search

参数是,

1. token 
2. term

图 2 - 当我开始像 "ma" 这样输入时,然后在后台 api 调用必须调用, 即 API 调用就像

url/collaborator/search?token=kdkhu67tdndodAK803i939ndAJDEw & term=ma

所以它会给出响应,并且该响应必须显示为如图所示的下拉列表

图 3 - 如果输入特定的名称,那么它必须根据其数据过滤电子邮件,并且必须提供特定的电子邮件,当我点击它时它必须添加文本字段。我想要单个搜索以及多个电子邮件搜索。

url/collaborator/search?token=kdkhu67tdndodAK803i939ndAJDEw & term=komal

目前我想一次实现一个,如果不止一个可以做或添加那么没问题。

那么,当我开始在文本字段中输入并获取数组中的数据时,如何在 运行 时调用 api ????

很简单,为此使用 UItextField 操作。

在 viewDidLoad 中添加这一行

txtSearch.addTarget(self, action: #selector(self.textFieldValueChanged(_:)), for: UIControl.Event.editingChanged)

每次文本字段值更改时,您都会在此函数中得到结果

@objc func textFieldValueChanged(_ textField: UITextField)
{
    print(textField.text ?? "")
    //call your api here
}

你可以通过一些技巧来做到这一点,我经常使用的方法如下

将编辑更改事件添加到您的文本字段,因为它会告诉您文本已更改

[_txtField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];


- (void)textFieldDidChange :(UITextField *) textField {
    if ([textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]].length>0) {
        // make you api call
    }
}

编辑:

要显示您的结果,您可以使用 table 视图。或类似 SearchTextField or MLPAutoCompleteTextField

写下代码,

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSLog(@"Data is : %@",_ccTextField.text);


    [self collaboratorApiMethod:_ccTextField.text];

    return YES;
}


// This metod is used to add collaborator, it will call API according to enetered data, JSON will receive
-(void)collaboratorApiMethod:(NSString*)valueFromTextField
{
   NSString *url =[NSString stringWithFormat:@"%@/collaborator/search?token=%@&term=%@",[userDefaults objectForKey:@"token"],valueFromTextField];
   // so this will generate in url

   // call api (REST API call)
   // you will get JSON data, store in array/dictionaries
}

假设您有一个包含电子邮件列表的数组。现在,当用户开始在 textField 中输入内容时,您必须在电子邮件中显示建议,并根据其值过滤并显示建议。

写下代码,

- (UITableViewCell *)autoSuggestionField:(UITextField *)field tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath forText:(NSString *)text {

  userSearchDataCell *cell=[tableView dequeueReusableCellWithIdentifier:@"userSearchDataCellId"];

    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"userSearchDataCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    NSArray *emails = emailArray;

   if (text.length > 0) {
        NSPredicate *filterPredictate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[cd] %@",text];
        months = [emailArray filteredArrayUsingPredicate:filterPredictate];
   }


    cell.emailLabel.text = emails[indexPath.row];
}


 - (NSInteger)autoSuggestionField:(UITextField *)field tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section forText:(NSString *)text {

    if (text.length == 0) {
        return emailArray.count;
    }

    NSPredicate *filterPredictate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[cd] %@", text];
     NSInteger count = [emailArray filteredArrayUsingPredicate:filterPredictate].count;
    return count;

}


- (CGFloat)autoSuggestionField:(UITextField *)field tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath forText:(NSString *)text {
    return 65;
}


- (void)autoSuggestionField:(UITextField *)field tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath forText:(NSString *)text {
    // NSLog(@"Selected suggestion at index row - %ld", (long)indexPath.row);

    NSArray *emails = emailArray;

    if (text.length > 0) {
        NSPredicate *filterPredictate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[cd] %@", text];
        months = [emailArray filteredArrayUsingPredicate:filterPredictate];
    }

   self.emailLabel.text =  emails[indexPath.row];

}