如何使用搜索字符串预加载UISearchDisplayController

时间:2013-01-22 15:10:46

标签: ios uisearchbar uisearchdisplaycontroller

我正在尝试使用searchString预加载UISearchBar。

在目前的设置中。我正在UITableViewController中使用UISearchDisplayController。

我使用的第一种方法是通过以下方法设置搜索字符串:

- (void) presetSearchDisplayControllerWithString:(NSString*)searchString{
    [self.searchDisplayController setActive:YES animated:NO];
    [self.tableView setContentOffset:CGPointMake(0, 0)];
    self.searchBar.text = searchString;
    self.searchFetchedResultsController = [self   createFetchedResultsControllerWithSearchTerm:searchString];
}

有效。 UISearchbar显示搜索字符串,隐藏原始tableView和TableViewIndex。但如果我按下searchBar上的取消按钮就会崩溃:

*断言失败 - [UISearchDisplayController setActive:animated:],/ SourceCache / UIKit_Sim / UIKit-2372 / UISearchDisplayController.m:682 由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'搜索内容导航控制器不能在-setActive:YES和-setActive:NO' *之间切换

第二个varian无法正常工作

- (void) searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller {
    [self.searchDisplayController.searchBar setText:@"rune"];
}

嗯,如果你点击搜索栏就行了。但是在你完成它之前,它只显示一个常规的tableView。

1 个答案:

答案 0 :(得分:0)

您是否尝试过这样的事情(仅基于引发的异常):

[self.searchDisplayController setActive:NO animated:NO];
self.searchBar.text = searchString;
[self.searchDisplayController setActive:YES animated:NO];
[self.tableView setContentOffset:CGPointMake(0, 0)];
self.searchFetchedResultsController = [self createFetchedResultsControllerWithSearchTerm:searchString];