【发布时间】:2015-01-26 22:54:50
【问题描述】:
我有一个应用程序可以从主屏幕上的搜索功能中受益。我已经编写了一些代码可以完成这项任务,但是它找不到结果,并且在点击 UISearchBar 上的 (x) 时崩溃。我一直在绞尽脑汁想弄清楚为什么它不起作用。
主视图控制器(主屏幕上有“搜索”栏):
ViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"CenterViewController"];
[self.navigationController pushViewController:viewController animated:YES];
[viewController sendSearchRequest:[nameLabel text] sport:[sportLabel text]];
ViewController(使用 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];
}
【问题讨论】:
标签: ios cocoa-touch uisearchbar uisearchdisplaycontroller