未调用 searchBarCancelButtonClicked 委托方法。有什么办法可以打电话吗?
UIViewController 实现一个放置 UITableView UISearchBar 的视图并被实现。但你不能调用searchBarCancelButtonClicked。不知道什么原因。
UIViewController to implement a view to placing an UITableView UISearchBar and being implemented. But you can not call searchBarCancelButtonClicked. Do not know what the reason.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
searchBarCancelButtonClicked 不触发的原因可能是因为您的 UISearchBar 没有显示“取消”按钮。您可以像这样显示“取消”按钮:
我遇到了同样的问题,并且显示“取消”按钮使 searchBarCancelButtonClicked 按预期触发。
The reason why searchBarCancelButtonClicked does not fire may be because your UISearchBar does not show the Cancel button. You can display the Cancel button like this:
I had the same problem and making the Cancel button show made searchBarCancelButtonClicked fire as expected.
确保您的 UIViewController 被设置为 UISearchBar 的委托,并且它采用 UISearchBarDelegate 协议。
Ensure that your UIViewController is set as the delegate of your UISearchBar, and that it adopts the UISearchBarDelegate protocol.
希望有帮助。
Hope it helps.
就我而言,是不受控制的 userInteractionEnabled=NO
In my case was an uncontrolled userInteractionEnabled=NO
searchController.searchBar.delegate = self
,不仅仅是searchController.delegate = self
searchController.searchBar.delegate = self
, not justsearchController.delegate = self
对我来说真正的原因是我认为小 x 按钮是取消按钮,但事实并非如此...如果您遇到我遇到的相同问题,请使用下面的代码
The real reason for me was that i thought the small x button was the meant cancel button, but it's not... if you are having the same issue i faced use the below code
此行删除 UISearchController 透明视图
This line remove UISearchController transparent view
尝试将其放入 viewDidLoad 中。就我而言,有时取消按钮操作委托“searchBarCancelButtonClicked”已触发,有时则没有(只是键盘被关闭并且取消按钮变得不活动),添加此后一切都开始按预期工作。
Try to put this in viewDidLoad. In my case sometimes Cancel button action delegate "searchBarCancelButtonClicked" has triggered and sometimes hasn't ( just keyboard dismissed and cancel button become not active), after adding this everything started working as expected.
Swift 4.2
我遇到了同样的问题,必须将 DefinePresentationContext 属性设置为 true,然后 searchBarCancelButtonClicked 方法才能工作。
Swift 4.2
I had the same problem and had to set the definesPresentationContext property to true before the searchBarCancelButtonClicked method would work.
当我在 UIScrollView 中有一个 UISearchBar 时,我遇到了同样的问题。据我了解,您将其放在 UITableView 中,它是 UIScrollView 的子类。
对我的情况有帮助的是设置
似乎
searchBarCancelButtonClicked
方法在设置为YES
(默认设置)时效果不佳。I ran into the same problem when I had a UISearchBar inside of a UIScrollView. As I understand the question you had it inside a UITableView, which is a subclass of UIScrollView.
What helped in my case was setting
Seems the
searchBarCancelButtonClicked
method doesn't work well with that set toYES
, which is the default setting.