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