【问题标题】:iOS : How to call an API runtime when I start typing in UITextfield?iOS:当我开始输入 UITextfield 时如何调用 API 运行时?
【发布时间】:2018-05-09 02:49:30
【问题描述】:

这是一个例子: 图片 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 ?????

【问题讨论】:

  • 您可以在文本字段委托方法中调用 shouldChangeCharactersInRange 但只要您键入单个关键字,它就会需要更多负载。因此,当您编写单个关键字或擦除时,每次 api 调用
  • 你能告诉我,如何在 shouldChangeCharactersInRange 方法中调用 API,我将在哪里获得响应以及如何使用该响应来显示结果?

标签: ios objective-c api uitextfield


【解决方案1】:

写下面的代码,

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

}

【讨论】:

  • 很好,现在只需在文本字段下方提供建议
【解决方案2】:

您可以通过一些棘手的方式来做到这一点,我经常使用的一种如下所示

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

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


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

编辑:

为了显示您的结果,您可以使用表格视图。或类似SearchTextFieldMLPAutoCompleteTextField

【讨论】:

    【解决方案3】:

    非常简单,为此使用 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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-02
      • 1970-01-01
      • 2017-08-09
      • 1970-01-01
      相关资源
      最近更新 更多