苹果文档 - UISearchController的介绍

从这段介绍,可以知道这几个重点:
1、UISearchController 的 delegate 和 searchResultsUpdater 可以设置为其他控制器;
2、UISearchController 弹出的搜索结果控制器和显示被搜索内容控制器之间是模态的关系,且 modalPresentationStyle 属性是 UIModalPresentationCurrentContext;
3、UISearchController 不应该被直接展示;


A UISearchController object manages the display of search results based on interactions with a search bar. You use a search controller in tandem with your existing view controllers. When you have a view controller with searchable content, incorporate the search bar of a UISearchController
object into your view controller’s interface. When the user interacts with that search bar, the search controller automatically displays a new view controller with the search results that you specify.
一个UISearchController对象管理用户操作 search bar 后的搜索结果。搜索控制器层叠在已有的视图控制器上面。当你有一个可搜索内容的视图控制器时,将UISearchController 对象的 search bar 组合到这个视图控制器的界面上。在用户操作 search bar 时,搜索控制器自动显示一个新的视图控制器,它包含了你指定的搜索内容。

A search controller works with two custom view controllers that you provide. The first view controller displays your searchable content and the second displays your search results. The first view controller is part of your app’s main interface and you display it in whatever way is appropriate for your app. You pass the second view controller to the initWithSearchResultsController:
method when you initialize your search controller, and the search controller displays that view controller at appropriate times.
一个搜索控制器和你提供的两个自定义视图控制器一起工作。第一个视图控制器显示可搜索内容,第二个显示搜索结果。第一个视图控制器是 App 主界面的一部分,它在 App 中可能以任何合适的方式显示。在初始化搜索控制器时,将第二个视图控制器传给initWithSearchResultsController:方法,这样搜索控制器就能在合适的时间显示这个视图控制器。

Each search controller provides a UISearchBar
object that you must incorporate into the user interface of your initial view controller. Add this object to the view containing your searchable contents. For example, if you use a table view for your searchable contents, you can assign the search bar to the tableHeaderView
property of that table view. When the user taps the search bar to enter a search term, the search controller automatically displays your search results view controller and notifies your app that the search process has begun.
每个搜索控制器都提供了UISearchBar对象,必须将它加到初始视图控制器的界面上。 把它添加到包含可搜索内容的视图上。例如,如果你的可搜索内容是一个表视图,你可以把 search bar 赋值给表视图的tableHeaderView属性。当用户点击 search bar 并输入搜索条目时,搜索控制器自动显示你的搜索结果视图控制器,同时通知你的 App 搜索已经开始了。

When the user interacts with the search bar, the search controller notifies the object in its searchResultsUpdater
property. You provide the search results updater object, which must conform to the UISearchResultsUpdating
protocol. You use the methods of that protocol to search your content and deliver the results to your search results view controller. Typically, the view controller with your searchable content also acts as the search results updater object, but you can use another object if you prefer.
当用户操作 search bar 时,搜索控制器通知它自己的属性searchResultsUpdater的对象。你要提供更新搜索结果的对象,它必须遵循UISearchResultsUpdating协议。你可以使用协议中的方法搜索你的内容并将结果传递给搜索结果视图控制器。通常,由可搜索内容的视图控制器担任更新搜索结果的对象,但是你也可以使用其他对象。

Listing 1 shows how to configure a search controller from the view controller displaying the searchable content. This code creates another custom view controller for displaying the search results and uses that object to create the search controller object. The current view controller stores a reference to the search controller and handles updates when the search term changes. The view controller also acts as the presentation context for the search results, which is usually what you want.
代码1 显示了如何在可搜索内容的视图控制器上配置一个搜索控制器。下面的代码创建了一个自定义视图控制器用来显示搜索结果并用它来创建一个搜索控制器对象。当前视图控制器保存了搜索控制器的引用且在搜索条目改变时更新界面。这个视图控制器也可以作为搜索结果的展示上下文。

Listing 1Creating and configuring a search controller

// Create the search results controller and store a reference to it.

MySearchResultsController* resultsController = [[MySearchResultsController alloc] init];

self.searchController = [[UISearchController alloc] initWithSearchResultsController:resultsController];

// Use the current view controller to update the search results.

self.searchController.searchResultsUpdater = self;

// Install the search bar as the table header.

self.tableView.tableHeaderView = self.searchController.searchBar;

// It is usually good to set the presentation context.

self.definesPresentationContext = YES;

To customize the presentation or dismissal of the search results controller, assign an object to the search controller’s delegate
property. Delegate objects must conform to the UISearchControllerDelegate
protocol. You use the methods of that protocol to be notified when the search controller itself is activated and when the search results controller is presented or dismissed.
为了能够自定义展示或者dismiss 搜索结果控制器,给搜索控制器的delegate属性分配一个对象。委托对象必须遵循UISearchControllerDelegate协议。搜索控制器在激活时、展示后、dismiss 后会通知协议中的方法。

NOTE
Although a UISearchController object is a view controller, you should never present it directly from your interface. If you want to present the search results interface explicitly, wrap your search controller in a UISearchContainerViewController object and present that object instead.

注意:尽管一个UISearchController对象是视图控制器,但是你绝对不能直接展示它。如果你希望精确的展示搜索结果界面,将你的搜索控制器包裹在UISearchContainerViewController对象中,然后展示这个对象。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,117评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,963评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 107,897评论 0 240
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,805评论 0 203
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,208评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,535评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,797评论 2 311
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,493评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,215评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,477评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,988评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,325评论 2 252
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,971评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,055评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,807评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,544评论 2 271
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,455评论 2 266

推荐阅读更多精彩内容