Ios 使用不带故事板的UISearchController

Ios 使用不带故事板的UISearchController,ios,uitableview,uisearchcontroller,Ios,Uitableview,Uisearchcontroller,我对iOS比较陌生(100小时左右),我正在尝试实现一个没有故事板的UISearchController。有可能通过编程实现吗?您必须通过编程实现UISearchController,因为苹果还没有提供使用故事板进行设置的功能 以下是在视图控制器中实现UISearchController的步骤 符合UISearchResultsUpdate协议 创建一个变量以引用UISearchController var searchController: UISearchController! 在vi

我对iOS比较陌生(100小时左右),我正在尝试实现一个没有故事板的UISearchController。有可能通过编程实现吗?

您必须通过编程实现
UISearchController
,因为苹果还没有提供使用故事板进行设置的功能

以下是在视图控制器中实现
UISearchController
的步骤

  • 符合
    UISearchResultsUpdate
    协议
  • 创建一个变量以引用
    UISearchController

    var searchController: UISearchController!
    
  • viewdiload(:)

  • 实现方法
    updateSearchResultsForSearchController(:)
    ,当搜索栏成为第一响应者或当用户更改搜索栏内的文本时调用该方法

    func updateSearchResultsForSearchController(searchController: UISearchController) {
        // No need to update anything if we're being dismissed.
        if !searchController.active {
            return
        }
    
        // you can access the text in the search bar as below
        filterString = searchController.searchBar.text
    
        // write some code to filter the data provided to your tableview
    }
    

太好了,如果您能在答案中添加UISearchBar的委托方法,以避免与管理搜索栏混淆,那就太好了。在以下答案中引用了它们:。谢谢什么是tableView?除了Praveens的详细答案之外,如果您需要Objective-C示例,Apple还提供了在iOS8中处理UISearchController的方法。
func updateSearchResultsForSearchController(searchController: UISearchController) {
    // No need to update anything if we're being dismissed.
    if !searchController.active {
        return
    }

    // you can access the text in the search bar as below
    filterString = searchController.searchBar.text

    // write some code to filter the data provided to your tableview
}