searchDisplayController 在 iOS 8 中被弃用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25826332/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
searchDisplayController deprecated in iOS 8
提问by George Asda
How do you correct the following so no warnings appear? What am I missing?
您如何更正以下内容,以免出现警告?我错过了什么?
When correcting the searchResultsController
to searchController
it gives me an error "object not found"
当修正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;
}
回答by michaelsnowden
The
UISearchController
class replaces theUISearchDisplayController
class for managing the display of search-related interfaces.
本
UISearchController
类替代UISearchDisplayController
类管理与搜索相关的接口的显示器。
Source : https://developer.apple.com/library/ios/releasenotes/General/WhatsNewIniOS/Articles/iOS8.html
来源:https: //developer.apple.com/library/ios/releasenotes/General/WhatsNewIniOS/Articles/iOS8.html
So, as rmaddy said, if you want to get rid of the deprecated warnings, stop using the deprecated classes. Use UISearchController
instead:
因此,正如 rmaddy 所说,如果您想摆脱已弃用的警告,请停止使用已弃用的类。使用UISearchController
来代替:
回答by RStack
Add this to your .h file
将此添加到您的 .h 文件中
<UISearchBarDelegate,UISearchResultsUpdating>
NSArray *searchResultsArray;
NSMutableArray *userMutableArray;
@property (retain, nonatomic) UISearchController *searchController;
and this to your .m file
这到您的 .m 文件
userMutableArray = [[NSMutableArray alloc] initWithObjects:@"Hyman",@"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;
}
回答by Gabriel Tomitsuka
I was looking to migrate from UISearchDisplayController
to UISearchController
. So, I found this on GitHub: https://github.com/dempseyatgithub/Sample-UISearchController
Download/clone it and you'll be able to see how to use UISearchController
with UITableView
and UICollectionView
.
It has everything you need to upgrade from UISearchDisplayController
to UISearchController
.
The UISearchController
documentation is also really helpful. For getting started, look at the Sample-UISearchController/MasterViewController_TableResults.m and Sample-UISearchController/MasterViewController_FilterResults.m
If you also need to support iOS 7(something I personally recommend if you are really about to deploy your app to the App Store) do this:
我希望从 迁移UISearchDisplayController
到UISearchController
. 所以,我发现这个在GitHub上:https://github.com/dempseyatgithub/Sample-UISearchController
下载/克隆它,你就可以看到如何使用UISearchController
与UITableView
和UICollectionView
。
它拥有从 升级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
}
Note: You'll have to do everything programatically if you want to support both versions of iOS.
注意:如果您想支持两个版本的 iOS,您必须以编程方式完成所有操作。
回答by Stephen Mlawsky
We've been working for a long time to get the new UISearchController to rotate properly.
我们一直在努力让新的 UISearchController 正确旋转。
Here's what it looked like before:
这是以前的样子:
After much time we essentially "gave up" on making the rotation work properly. Instead we simply hide the search controller before the rotation happens. The user then has to tap on the search button on the rotated view to bring up the search bar again.
很长一段时间后,我们基本上“放弃”了使旋转正常工作。相反,我们只是在旋转发生之前隐藏搜索控制器。然后用户必须点击旋转视图上的搜索按钮才能再次显示搜索栏。
Here's the relevant code:
这是相关的代码:
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
[self.searchController dismissViewControllerAnimated:YES completion:nil];
self.searchControllerWasActive = NO;
self.searchButton.enabled = YES;
}
Important note: Our code uses a UIViewController and not UITableViewController. The screen requires additional buttons which is why we cannot use UITableViewController. Using UISearchController on a UITableViewController does not exhibit the rotation issues.
重要提示:我们的代码使用 UIViewController 而不是 UITableViewController。屏幕需要额外的按钮,这就是我们不能使用 UITableViewController 的原因。在 UITableViewController 上使用 UISearchController 不会出现旋转问题。
We see this as a necessary work around given the current state of UISearchController. It would be much better to have a real solution to this problem.
鉴于 UISearchController 的当前状态,我们认为这是必要的解决方法。对这个问题有一个真正的解决方案会好得多。