UISearchDisplayController被iOS8废止,取而代之的是 UISearchController
本文转载自: http://my.oschina.net/u/1999967/blog/309511
苹果官方:UISearchController
0. initWithSearchResultsController:
可以为nil,表示使用当前的 viewController。也可以使用自定义的其他viewcontroller。
self.mySearchController = [[UISearchController alloc] initWithSearchResultsController:nil];
1.searchResultsUpdater:设置显示搜索结果的控制器。
1
|
_mySearchController.searchResultsUpdater = self;
|
2.dimsBackgroundDuringPresentation:设置开始搜索时背景显示与否
1
|
_mySearchController.dimsBackgroundDuringPresentation = NO;
|
3.[searchBar sizeToFit]:设置searchBar位置自适应
1
|
[_mySearchController.searchBar sizeToFit];
|
4.设置searchBar为UITableView的头部视图
1
|
self.myTableView.tableHeaderView = self.mySearchController.searchBar;
|
5.UISearchResultsUpdating:代理方法 -(
void
)updateSearchResultsForSearchController:(UISearchController *)searchController
话不多说,直接上代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
|
当需要使用Push,现实详细信息等情况的时候,需要将searchBar隐藏。可以将active属性设置为NO。
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
if ([segue.identifier isEqualToString:@"tablePush"])
{
if ([segue.destinationViewController isKindOfClass:[CodePushViewController class]])
{
CodePushViewController *destVC = (CodePushViewController *)segue.destinationViewController;
destVC.labelName = [showList objectAtIndex:[[tablesView indexPathForSelectedRow] row]];
searchC.active = NO ;
}
}
}