updatesearchresultsforsearchcontroller未调用

时间:2016-01-17 19:33:32

标签: ios iphone swift uisearchcontroller uisearchresultscontroller

有谁知道为什么不在这里打电话?谢谢。我想我正确设置了代表。

class LocationSearchController: UIViewController, UISearchResultsUpdating, UINavigationBarDelegate, UISearchControllerDelegate {

let navigationBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: UIScreen.mainScreen().bounds.width, height: 44))

var locationSearchController: UISearchController!

override func viewDidLoad() {
    super.viewDidLoad()


    locationSearchController = UISearchController(searchResultsController: nil)

    self.locationSearchController.loadViewIfNeeded()

    //sets up search controller
    self.locationSearchController.searchResultsUpdater = self
    self.locationSearchController.delegate = self
    definesPresentationContext = true
    //self.videoSearchController.searchBar.delegate = self

    //sets up nav bar
    navigationBar.backgroundColor = UIColor.blackColor()
    navigationBar.delegate = self



    navigationItem.titleView = locationSearchController.searchBar
    //leftButton.title = "Left"
    //navigationItem.leftBarButtonItem = leftButton
    navigationBar.items = [navigationItem]

    self.view.addSubview(navigationBar)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

func updateSearchResultsForSearchController(searchController: UISearchController) {
    print("search results updated 2")
}

}

任何想法都会非常感激。我已经搜索了一会儿,但我没有找到有关此问题的答案。

1 个答案:

答案 0 :(得分:4)

我遇到了与iOS8相同的问题。这是一种解决方法,但它会解决这个问题。

  • 让LocationSearchController实现UISearchBarDelegate
  • 在viewDidLoad中添加以下行:

    locationSearchController.searchBar.delegate = self
    
  • 添加此方法:

    func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
        updateSearchResultsForSearchController(locationSearchController)
    }