如何禁用/启用UISearchBar键盘的“搜索”按钮? [英] How can I disable/enable UISearchBar keyboard's Search button?

查看:329
本文介绍了如何禁用/启用UISearchBar键盘的“搜索”按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的代码中使用 UISearchBar 。我已经在头文件中导入了它的委托,并在实现文件中实现了一些委托方法。

I am using UISearchBar in my code. I have imported its delegate in header file and implemented some delegate methods in implementation file also.

当我们点击 UISearchBar ,键盘将出现输入文字。键盘的返回键是搜索按钮。它将默认禁用。当我们输入一个角色时,它将被启用。 (我是对的吗?)

When we tap on the UISearchBar, a keyboard will appear to enter text. The return key of the keyboard is "Search" button. It will disabled by default. When we enter a character, It will get enabled. (Am I right?)

这里的问题来了..我想启用 UISearchBar 键盘的返回键用户输入至少两个字母。

Here the problem comes.. I want to enable the UISearchBar keyboard's return key when the user types atleast two letters.

有可能吗?如果是,我们该怎么办?

Is it possible? If yes, how can we do it?

谢谢

推荐答案

您无法禁用搜索按钮。你可以做的是使用 UISearchBarDelegate 方法来判断你是否应该对点击的搜索按钮采取行动,如下所示:

You can't disable the search button. What you can do is use the UISearchBarDelegate methods to figure out if you should take action on the search button being clicked, like so:

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    if (searchBar.text.length < 2) {
        return;
    }
    else {
        // Do search stuff here
    }
}

Apple文档 for this非常有用,是自定义searchBar行为的一个很好的起点。

The Apple Documentation for this is very useful as well, and is a great starting point for customizing the searchBar's behavior.

这篇关于如何禁用/启用UISearchBar键盘的“搜索”按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆