所以我有 UISearchController,它显示栏,我可以输入文本但它不会触发委托(delegate)方法。这是我的代码:
@IBAction func searchTapped(AnyObject) {
NSLog("search...")
let cancelButton = UIBarButtonItem(image: UIImage(named: "delete_sign-50"), landscapeImagePhone: nil, style: .Plain, target: self, action: "cancelTapped:")
var searchController = UISearchController(searchResultsController: nil)
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "enter the text to search"
searchController.searchResultsUpdater = self;
searchController.hidesNavigationBarDuringPresentation = false
searchController.delegate = self
searchController.searchBar.delegate = self
self.definesPresentationContext = true;
UIView.animateWithDuration(0.2, animations: { () -> Void in
self.navigationItem.rightBarButtonItems = nil
self.navigationItem.rightBarButtonItems = [cancelButton]
self.navigationItem.titleView = searchController.searchBar
searchController.searchBar.sizeToFit()
self.toolbar.hidden = false
self.tableView?.hidden = false
})
//add first responser to search bar
searchController.searchBar.becomeFirstResponder()
}
func updateSearchResultsForSearchController(searchController: UISearchController) {
NSLog("1 %@", searchController.searchBar.text);
}
func searchBar(searchBar: UISearchBar, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
NSLog("2 %@", text)
return true
}
当文本更改时,这些方法都不会被调用。
除此之外,我还尝试将 searchController 作为变量添加到我的 Controller 中,在这种情况下,它的效果非常好——当我点击搜索栏时,它会移出屏幕。
最佳答案
searchController.active = YES;
这有帮助,不知道为什么没有在任何地方提到它。
关于ios - UISearchController 不触发委托(delegate)方法和搜索栏委托(delegate)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29394971/