![](/img/trans.png)
[英]UISearchBarDelegate function -searchBarSearchButtonClicked not working
[英]searchBarSearchButtonClicked not working
我已經在viewDidLoad()方法中設置了委托,並且還檢查了IB以查看使用(控制+拖動)時委托是否顯示出來,並且實際上確實顯示為已連接。 我也刪除了它,並在IB中完全添加了新的SearchBar並將其掛鈎。 似乎沒有辦法解決問題。 有什么建議么?
override func viewDidLoad() {
self.searchBar.layer.zPosition = 1
self.searchBar.delegate = self
}
//this is never being called, breakpoint never hits
func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
print("searchText \(searchText)")
}
//this is never being called, breakpoint never hits
func searchBarSearchButtonClicked(searchBar: UISearchBar) {
print("search button clicked")
self.firebaseQuery()
}
是否在Interface Builder中設置了自定義View Controller類? 即在“身份檢查器”選項卡上,確保在“自定義類”部分中設置了所需的UIViewController子類。
另外,嘗試在viewDidLoad()中設置一個斷點。 運行該應用程序,如果斷點沒有達到您的預期,這有助於縮小問題范圍。
感謝reddit用戶@applishish,我得到了這個問題
我沒有在參數-_-前面加下划線
感謝你的幫助!
您是否可能設置了錯誤的委托?
在Interface Builder中,命令單擊搜索欄,將委托拖到情節提要中的黃色ViewController按鈕以設置委托。 從viewDidLoad()
刪除self.searchBar.delegate = self
我也沒有看到您的viewController類是否正確符合UISearchBarDelegate
我將其添加到控制器的擴展中,並將所有與之相關的代碼放在此處。
class ViewController: UIViewController{...}
extension ViewController: UISearchBarDelegate {
// implement all searchBar related methods here:
func searchBarButtonClicked(_ searchBar: UISearchBar) {
//capture the string
if let input = searchBar.text {
// do something with string.
print(input)
}
}
使用IB進行設置時,我也沒有響應。 我最終使用代碼而不是IB進行了設置,並且成功了。 嘗試這種方式:
class MySearchTableViewController: UITableViewController, UISearchResultsUpdating, UISearchBarDelegate {
let searchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
super.viewDidLoad()
self.searchController.searchBar.autocapitalizationType = UITextAutocapitalizationType.none
self.navigationItem.searchController = self.searchController
//if this is set to true, the search bar hides when you scroll.
self.navigationItem.hidesSearchBarWhenScrolling = false
//this is so I'm told of changes to what's typed
self.searchController.searchResultsUpdater = self
self.searchController.searchBar.delegate = self
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
//hopefully this gets called when you click Search button
}
func updateSearchResults(for searchController: UISearchController) {
//if you want to start searching on each keystroke, you implement this method. I'm going to wait until they click Search.
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.