【问题标题】:searchDisplayController deprecated in iOS 8在 iOS 8 中不推荐使用 searchDisplayController
【发布时间】:2014-09-13 18:29:32
【问题描述】:

您如何更正以下内容以使不出现警告? 我错过了什么?

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

【问题讨论】:

  • 那可能是什么?请参阅上面编辑过的帖子...
  • 没有。您可以根据需要添加自己的 ivar 或属性来跟踪它。
  • 你在开玩笑吧?为什么我必须这样做才能完成一个一直免费提供的简单任务?苹果在想什么?

标签: ios uisearchdisplaycontroller


【解决方案1】:

UISearchController 类替换了UISearchDisplayController 用于管理搜索相关界面显示的类。

来源: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

【讨论】:

  • 如果 Apple 从 Interface Builder 中删除不推荐使用的类会很有帮助。
【解决方案2】:

将此添加到您的 .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。
  • 简单详细的回答。
【解决方案3】:

我希望从 UISearchDisplayController 迁移到 UISearchController。所以,我在 GitHub 上找到了这个:https://github.com/dempseyatgithub/Sample-UISearchController
下载/克隆它,您将能够看到如何使用 UISearchControllerUITableViewUICollectionView
它拥有从 UISearchDisplayController 升级到 UISearchController 所需的一切。
UISearchController 文档也很有帮助。要开始使用,请查看 Sample-UISearchController/MasterViewController_TableResults.m 和 Sample-UISearchController/MasterViewController_FilterResults.m
如果您还需要支持 iOS 7(如果您真的打算将应用部署到 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,您必须以编程方式完成所有操作。

【讨论】:

    【解决方案4】:

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

    这是之前的样子:

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

    以下是相关代码:

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

    重要提示:我们的代码使用 UIViewController 而不是 UITableViewController。屏幕需要额外的按钮,这就是我们不能使用 UITableViewController 的原因。在 UITableViewController 上使用 UISearchController 不会出现旋转问题。

    鉴于 UISearchController 的当前状态,我们认为这是一项必要的工作。最好有一个真正的解决方案来解决这个问题。

    【讨论】: