5

有谁知道为什么不在这里调用它?谢谢。我想我正确地设置了代表。

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")
}

}

任何想法将不胜感激。我已经搜索了一段时间,但没有找到有关此问题的答案。

4

1 回答 1

5

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

  • 让 LocationSearchController 实现 UISearchBarDelegate

  • 在 viewDidLoad 中添加这一行:

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

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

Objective C 使用这个

  • (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{ [self updateSearchResultsForSearchController:locationSearchController]; }
于 2016-04-29T20:18:49.940 回答