0
我有一个应用程序,将受益于主屏幕上的搜索功能。我已经编写了一些完成此任务的代码,但它不会找到结果,并且在UISearchBar上点击(x)时会崩溃。我一直在试图弄清楚为什么它不起作用。从另一个ViewController激活UISearchDisplayController
主视图控制器(与 '搜索' 主屏幕上栏):
ViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"CenterViewController"];
[self.navigationController pushViewController:viewController animated:YES];
[viewController sendSearchRequest:[nameLabel text] sport:[sportLabel text]];
视图控制器(查看与tableview中和UISearchDisplayController)
-(void)sendSearchRequest:(NSString *)request sport:(NSString *)sport{
[self.filteredArray removeAllObjects]; // First clear the filtered array.
self.searchText = request;
/*
* Search the main list for products whose type matches the scope (if
* selected) and whose name matches searchText; add items that match to the
* filtered array.
*/
[self.filteredArray removeAllObjects]; // First clear the filtered array.
self.searchText = request;
/*
* Search the main list for products whose type matches the scope (if
* selected) and whose name matches searchText; add items that match to the
* filtered array.
*/
NSLog(@"Request: %@", request);
for (Athlete *athlete in self.athletes) {
NSComparisonResult result = [[athlete name] compare:request options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [request length])];
if (result == NSOrderedSame) {
[self.filteredArray addObject:athlete];
}
}
[self.searchDisplayController setActive: YES animated: YES];
[self.searchDisplayController.searchBar setText:request];
}