ios - 如何停止 uisearch 即时搜索?

标签 ios objective-c uisearchbar

我正在使用 uisearchbar、tableview 和 JSON Web 服务使用以下代码进行搜索。当前的问题是,尽管应用程序正在搜索并在表格 View 中呈现数据,但它是在搜索栏中发生每个文本更改后进行搜索。我希望它仅在用户触摸“搜索”按钮后进行搜索。当它通过 uisearch 文本字段中的每一个更改来查询网络时,它似乎陷入了困境。

这是我的搜索方式:

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{

    NSString *searchQuery = [NSString stringWithFormat:@"http://api.****/%@",searchText];

    NSString *jsonString = [NSString
                            stringWithContentsOfURL:[NSURL URLWithString:searchQuery]
                            encoding:NSStringEncodingConversionAllowLossy|NSUTF8StringEncoding
                            error:nil];

    NSMutableArray *itemsTMP = [[NSMutableArray alloc] init];

    SBJSON *parser = [[SBJSON alloc] init];
    NSDictionary *results = [parser objectWithString:jsonString error:nil];

    itemsTMP = [results objectForKey:@"results"];

    //   [self setDisplayItems:[results objectForKey:@"results"]];

    [self setAllItems:[itemsTMP copy]];
    self.displayItems = [itemsTMP copy];

    int a =   [displayItems count];
    NSString *countString = [NSString stringWithFormat:@"results %d",a];
    countLabel.text = countString;

    [self.tableView reloadData];


}

我在想也许代码应该移动到

-(void) searchBarSearchButtonClicked:(UISearchBar *)aSearchBar {

    [self.searchBar resignFirstResponder];

    return;
    // searchBar resigns after search clicked

}

但不确定如何将 :(NSString *)searchText 添加到此方法

感谢您的帮助

最佳答案

只需将 textDidChange 代码转换为 searchBarSearchButtonClicked 方法即可解决您的问题。

-(void) searchBarSearchButtonClicked:(UISearchBar *)aSearchBar {

    [self.searchBar resignFirstResponder];

    NSString *searchQuery = [NSString stringWithFormat:@"http://api.****/%@",aSearchBar.text];

    NSString *jsonString = [NSString
                            stringWithContentsOfURL:[NSURL URLWithString:searchQuery]
                            encoding:NSStringEncodingConversionAllowLossy|NSUTF8StringEncoding
                            error:nil];

    NSMutableArray *itemsTMP = [[NSMutableArray alloc] init];

    SBJSON *parser = [[SBJSON alloc] init];
    NSDictionary *results = [parser objectWithString:jsonString error:nil];

    itemsTMP = [results objectForKey:@"results"];

    //   [self setDisplayItems:[results objectForKey:@"results"]];

    [self setAllItems:[itemsTMP copy]];
    self.displayItems = [itemsTMP copy];

    int a =   [displayItems count];
    NSString *countString = [NSString stringWithFormat:@"results %d",a];
    countLabel.text = countString;

    [self.tableView reloadData];

    return;
    // searchBar resigns after search clicked

}

关于ios - 如何停止 uisearch 即时搜索?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15001614/

相关文章:

ios - 是否可以在没有 Apple 开发者计划的情况下分发应用程序?

Swift:自定义搜索栏布局问题

javascript - NativeScript - 如何减少 iOS 的应用程序图标角标(Badge)编号

ios - 为什么 Interface Builder 中的 "inferred"设置不起作用?

objective-c - 如何找出谁调用了方法?

iphone - [MFMailComposeViewController canSendMail] 什么时候返回 NO

objective-c - Sprite 套件 Sprite 未被着色

xcode - SwiftUI 包装的 SearchBar 无法关闭键盘

ios - UITableViewController隐藏导航栏[搜索栏]

ios - 无法让 QuickBlox 示例模块在 iOS 中工作