未调用 searchBarCancelButtonClicked 委托方法。有什么办法可以打电话吗?

发布于 2024-10-06 03:47:45 字数 106 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

另类 2024-10-13 03:47:49

searchBarCancelButtonClicked 不触发的原因可能是因为您的 UISearchBar 没有显示“取消”按钮。您可以像这样显示“取消”按钮:

searchBar.showsCancelButton = true

我遇到了同样的问题,并且显示“取消”按钮使 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:

searchBar.showsCancelButton = true

I had the same problem and making the Cancel button show made searchBarCancelButtonClicked fire as expected.

小嗷兮 2024-10-13 03:47:49

确保您的 UIViewController 被设置为 UISearchBar 的委托,并且它采用 UISearchBarDelegate 协议。

Ensure that your UIViewController is set as the delegate of your UISearchBar, and that it adopts the UISearchBarDelegate protocol.

甜心 2024-10-13 03:47:49
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
   // This method has been called when u enter some text on search or Cancel the search.
   // so make some condition for example:

   if([searchText isEqualToString:@""] || searchText==nil) {
        // Nothing to search, empty result.
        searching = NO;
        [mytableView reloadData];
    }
}

希望有帮助。

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
   // This method has been called when u enter some text on search or Cancel the search.
   // so make some condition for example:

   if([searchText isEqualToString:@""] || searchText==nil) {
        // Nothing to search, empty result.
        searching = NO;
        [mytableView reloadData];
    }
}

Hope it helps.

紫罗兰の梦幻 2024-10-13 03:47:49

就我而言,是不受控制的 userInteractionEnabled=NO

In my case was an uncontrolled userInteractionEnabled=NO

伴随着你 2024-10-13 03:47:49

searchController.searchBar.delegate = self,不仅仅是searchController.delegate = self

searchController.searchBar.delegate = self, not just searchController.delegate = self

网名女生简单气质 2024-10-13 03:47:49

对我来说真正的原因是我认为小 x 按钮是取消按钮,但事实并非如此...如果您遇到我遇到的相同问题,请使用下面的代码

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        if searchText.isEmpty {
            searchRecords.removeAll()
            searchTableView.reloadData()
        }
    }

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

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        if searchText.isEmpty {
            searchRecords.removeAll()
            searchTableView.reloadData()
        }
    }
清风疏影 2024-10-13 03:47:49

此行删除 UISearchController 透明视图

let searchController = UISearchController(searchResultsController: nil)
searchController.dimsBackgroundDuringPresentation = false

This line remove UISearchController transparent view

let searchController = UISearchController(searchResultsController: nil)
searchController.dimsBackgroundDuringPresentation = false
虫児飞 2024-10-13 03:47:49

尝试将其放入 viewDidLoad 中。就我而言,有时取消按钮操作委托“searchBarCancelButtonClicked”已触发,有时则没有(只是键盘被关闭并且取消按钮变得不活动),添加此后一切都开始按预期工作。

UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).isEnabled = true

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.

UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).isEnabled = true
尛丟丟 2024-10-13 03:47:49

Swift 4.2

我遇到了同样的问题,必须将 DefinePresentationContext 属性设置为 true,然后 searchBarCancelButtonClicked 方法才能工作。

 definesPresentationContext = true

Swift 4.2

I had the same problem and had to set the definesPresentationContext property to true before the searchBarCancelButtonClicked method would work.

 definesPresentationContext = true
不…忘初心 2024-10-13 03:47:49

当我在 UIScrollView 中有一个 UISearchBar 时,我遇到了同样的问题。据我了解,您将其放在 UITableView 中,它是 UIScrollView 的子类。

对我的情况有帮助的是设置

tableView.delaysContentTouches = NO;

似乎 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

tableView.delaysContentTouches = NO;

Seems the searchBarCancelButtonClicked method doesn't work well with that set to YES, which is the default setting.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文