[英]Activate UISearchDisplayController from another ViewController
我有一個可以從主屏幕上的搜索功能中受益的應用程序。 我編寫了一些代碼,這些代碼在一半時就完成了此任務,但找不到結果,並且在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];
}
事實證明,這與正確激活無關。 基本上,我有一個要過濾的數組,但是在加載視圖時將其初始化。 好吧,由於我稱此視圖為一整輪,因此它從未下載陣列。 相反,我從主視圖控制器下載了數組,並在搜索視圖控制器中進行了搜索。 問題解決了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.