简体   繁体   English

UISearchcontroller的“取消”按钮不起作用

[英]UISearchcontroller Cancel button not working

class PlaceSelectorViewController: UIViewController, GMSAutocompleteResultsViewControllerDelegate, UISearchBarDelegate, UISearchControllerDelegate {

I have UISearchBarDelegate in my class 我班上有UISearchBarDelegate

searchController?.searchBar.delegate = self

and I had set the delegate to Self. 我已将代表设置为“自我”。

func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
    stopHighlight()
}

But this Delegate call is not working 但是此委托呼叫不起作用

You need an outleet for your UISearchBar (assuming you're working with Storyboard this is really easy, just drag and drop via Assistant Editor). 您需要为UISearchBar提供一个outleet (假设您正在使用Storyboard,这真的很简单,只需通过Assistant Editor拖放即可)。 and that's what you're going to add the delegate to. 这就是将委托添加到的内容。 In case you built programmatically, then that variable is the one that's going to get assigned to the delegate. 如果您以编程方式构建,则该变量将是分配给委托的变量。

Here's the example code for the Storyboard built 这是为Storyboard构建的示例代码

@IBOutlet weak var searchBar: UISearchBar! (you must see a filled circle at the right of this, otherwise there's no link between Storyboard and this outlet ) (您必须在此右侧看到一个实心圆圈,否则Storyboard和此插座之间没有链接)

... ...

override func viewDidLoad() { super.viewDidLoad() searchBar.delegate = self }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM