我已经使用嵌入的导航栏设置了搜索栏。未检测到func searchBarSearchButtonClicked。我想这样做,以便当用户点击搜索栏时,将调用另一个功能。我已经删除了一些与此问题无关的无关代码。可能是什么问题?

class FirstViewController: UIViewController, UISearchBarDelegate {

var resultSearchController: UISearchController? = nil

    override func viewDidLoad() {
    super.viewDidLoad()

    // set up the search results table
    let locationSearchTable = storyboard!.instantiateViewController(withIdentifier: "LocationSearchTable") as! LocationSearchTable

    resultSearchController = UISearchController(searchResultsController: locationSearchTable)
    resultSearchController?.searchResultsUpdater = locationSearchTable

    let searchBar = resultSearchController!.searchBar
    searchBar.sizeToFit()
    searchBar.placeholder = "Where would you like to go"
    navigationItem.titleView = resultSearchController?.searchBar
    searchBar.delegate = self

    resultSearchController?.hidesNavigationBarDuringPresentation = false
    resultSearchController?.dimsBackgroundDuringPresentation = true
    definesPresentationContext = true
    }

    func searchBarSearchButtonClicked(_: UISearchBar) {
    //  closeMapType()
    //  self.mapType.transform = .identity
    print("it worked")
  }
}

最佳答案

当用户点击搜索栏时,将调用以下功能:

func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool {
    return true
}


当我们点击键盘上的“搜索”按钮时,它将调用

func searchBarSearchButtonClicked(_: UISearchBar)

08-04 23:54