[英]Activate UISearchDisplayController from another ViewController
I have an app that would benefit from a search function on the main screen. 我有一个可以从主屏幕上的搜索功能中受益的应用程序。 I have written some code that halfway accomplishes this task, but it won't find results and it crashes when tapping (x) on the UISearchBar.
我编写了一些代码,这些代码在一半时就完成了此任务,但找不到结果,并且在UISearchBar上点击(x)时会崩溃。 I have been racking my brain trying to figure out why its not working.
我一直在绞尽脑汁想弄清楚为什么它不起作用。
Main View Controller (with 'search' bar on main screen): 主视图控制器(在主屏幕上带有“搜索”栏):
ViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"CenterViewController"];
[self.navigationController pushViewController:viewController animated:YES];
[viewController sendSearchRequest:[nameLabel text] sport:[sportLabel text]];
ViewController (view with tableview and UISearchDisplayController) 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];
}
Turns out, it had nothing to do with proper activation. 事实证明,这与正确激活无关。 Basically, I had an array that I was filtering, but it was initialized when the view loaded.
基本上,我有一个要过滤的数组,但是在加载视图时将其初始化。 Well, since I was calling the view very round-a-bout, it never downloaded the array.
好吧,由于我称此视图为一整轮,因此它从未下载阵列。 Instead I downloaded the array from the Main view controller and searched that in the search view controller.
相反,我从主视图控制器下载了数组,并在搜索视图控制器中进行了搜索。 Problem solved.
问题解决了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.