searchDisplayController在iOS 8中已弃用

如何纠正以下内容,以免出现警告? 我错过了什么?

当更正searchResultsController searchController它给了我一个错误“找不到对象”

 if (tableView == self.searchDisplayController.searchResultsTableView) { cell.textLabel.text = [searchResults objectAtIndex:indexPath.row]; } else { cell.textLabel.text = [_content objectAtIndex:indexPath.row]; } return cell; } -(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString { [self filterContentForSearchText:searchString scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]]; return YES; } 

UISearchController类replaceUISearchDisplayController类来pipe理与search相关的接口的显示。

资料来源: https : //developer.apple.com/library/ios/releasenotes/General/WhatsNewIniOS/Articles/iOS8.html

所以,正如rmaddy所说的那样,如果你想摆脱不推荐的警告,停止使用已弃用的类。 改用UISearchController

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UISearchController/index.html#//apple_ref/occ/cl/UISearchController

将此添加到您的.h文件

 <UISearchBarDelegate,UISearchResultsUpdating> NSArray *searchResultsArray; NSMutableArray *userMutableArray; @property (retain, nonatomic) UISearchController *searchController; 

这对你的.m文件

 userMutableArray = [[NSMutableArray alloc] initWithObjects:@"Jack",@"Julie", nil]; searchResultsArray = [[NSArray alloc]init]; self.searchController = [[UISearchController alloc]initWithSearchResultsController:nil]; self.searchController.searchBar.scopeButtonTitles = [[NSArray alloc]initWithObjects:@"UserId", @"JobTitleName", nil]; self.searchController.searchBar.delegate = self; self.searchController.searchResultsUpdater = self; [self.searchController.searchBar sizeToFit]; self.searchController.dimsBackgroundDuringPresentation = NO; self.definesPresentationContext = YES; self.tableView.tableHeaderView = self.searchController.searchBar; -(void)updateSearchResultsForSearchController:(UISearchController *)searchController{ NSString *searchString = self.searchController.searchBar.text; NSPredicate *resultPredicate; NSInteger scope = self.searchController.searchBar.selectedScopeButtonIndex; if(scope == 0){ resultPredicate = [NSPredicate predicateWithFormat:@"userId contains[c] %@",searchString]; }else{ resultPredicate = [NSPredicate predicateWithFormat:@"jobTitleName contains[c] %@",searchString]; } searchResultsArray = [userMutableArray filteredArrayUsingPredicate:resultPredicate]; [self.tableView reloadData]; } - (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope{ [self updateSearchResultsForSearchController:self.searchController]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if(self.searchController.active){ return [searchResultsArray count]; }else{ return [userMutableArray count]; } } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellIdentifier; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if(!cell){ cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; } if(self.searchController.active){ cell.textLabel.text = [searchResultsArray objectAtIndex:indexPath.row]; }else{ cell.textLabel.text = [userMutableArray objectAtIndex:indexPath.row]; } return cell; } 

我正在从UISearchDisplayController迁移到UISearchController 。 所以,我在GitHub上find了这个: https : //github.com/dempseyatgithub/Sample-UISearchController
下载/克隆它,你将能够看到如何使用UITableViewUICollectionView UISearchController
它拥有从UISearchDisplayController升级到UISearchDisplayController所需的一切。
UISearchController文档也很有帮助。 要开始,请查看Sample-UISearchController / MasterViewController_TableResults.m和Sample-UISearchController / MasterViewController_FilterResults.m
如果您还需要支持iOS 7(我个人build议,如果您真的要将应用程序部署到App Store),请执行以下操作:

 if([UISearchController class]){ //Create an UISearchController and add it to your UITableViewController }else{ //Create an UISearchDisplayController and add it to your UITableViewController } 

注意:如果您想要支持这两个版本的iOS,您将不得不以编程方式执行所有操作。

我们一直在努力使新的UISearchController正确旋转。

以下是它的样子:

经过很长时间,我们基本上“放弃”了使旋转正常工作。 相反,我们只是在旋转发生之前隐藏search控制器。 用户然后必须点击旋转视图上的searchbutton来再次调出search栏。

以下是相关的代码:

 - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator { [self.searchController dismissViewControllerAnimated:YES completion:nil]; self.searchControllerWasActive = NO; self.searchButton.enabled = YES; } 

重要说明:我们的代码使用UIViewController而不是UITableViewController。 屏幕需要额外的button,这就是为什么我们不能使用UITableViewController。 在UITableViewController上使用UISearchController不会出现旋转问题。

鉴于UISearchController的当前状态,我们将此视为一项必要的工作。 真正解决这个问题要好得多。