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

标签 ios objective-c api uitextfield

这是一个例子: enter image description here 图像 1 - 它包含一个 UITextfield(例如 emailTextField)。当我开始输入时,根据后台的关键字,将调用一个 API,该 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 ???

最佳答案

编写下面的代码,

- (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
}

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

编写下面的代码,

- (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];

}

关于iOS : How to call an API runtime when I start typing in UITextfield?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47484267/

相关文章:

ios - 如果执行操作时间过长,则退出无限循环

ios - 替代 AVFoundation captureStillImageAsynchronouslyFromConnection

ios - 快速本地通知警报声

objective-c - 在 NSTextView 中填充——没有自定义绘图可能吗?

iphone - 如何测试 iOS 推送通知?

ios - 如何将私钥添加到分发证书中?

objective-c - iOS 中带有结构的 XML-RPC 请求

python - 开始学习新的 Python 3.5 Asyncio(协程)的好地方 | Discord.py BOT 崩溃

java weka stringtoword vector 没有正确计算单词出现次数

android - 如何通过 REST API 传输图像