请选择 进入手机版 | 继续访问电脑版

技术控

    今日:208| 主题:54143
收藏本版 (1)
最新软件应用技术尽在掌握

[其他] UISearchController Development Guide

[复制链接]
容颜出现编译错误 发表于 2016-11-28 17:09:25
151 3

立即注册CoLaBug.com会员,免费获得投稿人的专业资料,享用更多功能,玩转个人品牌!

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
UISearchControlleris a new controller in iOS 8 to handle search.  
  Prior to iOS 8, we have    UISearchDisplayController, which is deprecated.    UISearchControllermore than replaced it, with a architectural change.  
  The Architecture

  There are two View Controllers (VC) involved in search. Let’s call them:
  
       
  •       Presenting VC- where the search is triggered   
  •       Results VC- where the results are displayed in  
  You may also have the presenting VC display the results. In that case, they are the same VC.
      UISearchControlleralso provides a    UISearchBarobject, because all search requires the search bar for input.  
  Triggering the search

  There are 2 ways.
  1. Add Search Bar

  Add the    UISearchBarto your presenting VC.  
  This must be done programatically, because there is no library object for    UISearchControllerin Xcode. And you have to use it’s    searchBarobject.  
  If you are using a    UITableView, a search bar can be added to the header easily:  
  1. tableView.tableHeaderView = searchController.searchBar
复制代码
2. Add Search Button

  Another way is to have a button to trigger the search, instead of adding the whole search bar in like in (1).
  Create the    IBActionfor the button:  
  1. @IBAction func tapSearch(sender: AnyObject) {
  2.     presentViewController(searchController, animated: true, completion: nil)
  3. }
复制代码
Remember:    searchControlleris a    UIViewController(read in later section) so it can be called with    presentViewController.  
  When the presenting VC has a navigation bar, you will need to configure search controller:
  1. searchController.hidesNavigationBarDuringPresentation = false
复制代码
The Delegates

  Delegate #1 - UISearchResultsUpdating

            UISearchResultsUpdating    protocol has a callback when the user enters into the search bar.  
  Set    searchResultsUpdater. Typically, the results VC will implement the protocol, so that it will update the results accordingly.  
  1. searchController.searchResultsUpdater = resultsViewController
复制代码
Then in    resultsViewController, implement the method:  
  1. func updateSearchResultsForSearchController(searchController: UISearchController) {
  2.         let searchTerm = searchController.searchBar.text
  3.         // Update your results
  4. }
复制代码
Delegate #2 - UISearchBarDelegate

            UISearchBarDelegate    protocol provides more events:  
  
       
  • text changed   
  • should text change   
  • should/begin/end editing   
  • tap on cancel button/etc  
  Set    searchBar.delegate. Typically, the results VC.  
  1. searchController.searchBar.delegate = resultsViewController
复制代码
You might be thinking we have    searchResultsUpdater. Isn’t that enough? Usually so, unless you want to know when buttons such as    scopes buttonare tapped on.  
  Delegate #3 - UISearchControllerDelegate

            UISearchControllerDelegate    protocol provides events when:  
  
       
  • the search controller is presented or dismissed  
  Set    delegateto the view controller that handles the calls, typically the presenting VC.  
  1. searchController.delegate = self
复制代码
UISearchController is a UIViewController

      UISearchControllerinherits from    UIViewController.  
  You can present it modally with    presentViewController.  
  BUT, you should never push to navigation controller or use it as a child etc. If you want that, you can use    UISearchContainerViewControllerto wrap it first.



上一篇:你是一个走在前沿的 Java 开发者吗?
下一篇:Critical Alerting for When Your Tools Are Out of Control
烨然少爷 发表于 2016-11-28 21:14:45
走过,路过,千万不要错过,顶一下!
回复 支持 反对

使用道具 举报

高考 发表于 2016-11-30 19:23:12
明明回贴贴
回复 支持 反对

使用道具 举报

邓永超 发表于 2016-12-26 06:05:26
看了这么多帖子,第一次看到这么经典的!
回复 支持 反对

使用道具 举报

*滑动验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

我要投稿

推荐阅读


回页顶回复上一篇下一篇回列表
手机版/CoLaBug.com ( 粤ICP备05003221号 | 文网文[2010]257号 )

© 2001-2017 Comsenz Inc. Design: Dean. DiscuzFans.

返回顶部 返回列表