单击输入按钮或搜索按钮时隐藏键盘

发布于 2024-12-01 18:34:02 字数 457 浏览 2 评论 0原文

我已在工具栏按钮内的 UIBarButtonItem 内添加了 UISearchBar 按钮。

在下面的形式中:

// search bar
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 260, 44)];
UIBarButtonItem *searchBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:searchBar];
[searchBar release];

[buttons addObject:searchBarButtonItem];

现在,当我单击 UISearchBar 键盘时出现问题。我想在单击输入或搜索按钮时隐藏键盘。我该怎么做?

I've added the UISearchBar button inside the UIBarButtonItem inside the toolbar button.

In the below form:

// search bar
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 260, 44)];
UIBarButtonItem *searchBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:searchBar];
[searchBar release];

[buttons addObject:searchBarButtonItem];

Now the problem in when I click on the UISearchBar keyboard appears. I'd like to hide the keyboard on clicking in enter or search button. How can i do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

凉世弥音 2024-12-08 18:34:02

实现 UISearchBarDelegate 中的方法:

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    [searchBar resignFirstResponder];
}

Implement a method from UISearchBarDelegate:

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    [searchBar resignFirstResponder];
}
贱人配狗天长地久 2024-12-08 18:34:02

对于斯威夫特

func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
    searchBar.resignFirstResponder()
}

For Swift

func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
    searchBar.resignFirstResponder()
}
笑着哭最痛 2024-12-08 18:34:02

您需要处理 UISearchBarDelegate 协议 方法。

在以下方法中,调用UISearchBar的-resignFirstResponder方法。

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar

上述委托方法的详细信息可以在 苹果官方文档。我经常在搜索结束时辞去这些委托方法中的第一响应者。

You need to process the UISearchBarDelegate protocol methods.

In the following methods, call -resignFirstResponder method of UISearchBar.

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar

The detail of above delegate methods can be found in the Apple official document. I often resign first responder inside those delegate methods as the end of searching.

我不是你的备胎 2024-12-08 18:34:02

您可以使用 UIEarchBar 的委托方法

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
   [searchBar resignFirstResponder];
}

并输入(未测试)

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText;   // called when text changes (including clear)
{
    if ([searchText isEqualToString:@"\n"])
               [searchBar resignFirstResponder];
}

希望这可以帮助您...

you can use UISEarchBar's delegate method

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
   [searchBar resignFirstResponder];
}

and for enter (not tested)

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText;   // called when text changes (including clear)
{
    if ([searchText isEqualToString:@"\n"])
               [searchBar resignFirstResponder];
}

Hope this helps you...

暮光沉寂 2024-12-08 18:34:02

在头文件中,添加或连接到 UISearchBar 出口:应如下所示:

@property (weak, nonatomic) IBOutlet UISearchBar *searchBarName;

添加以下内容

self.searchBarName.delegate = self;

然后,在实现器中,在 viewDidLoad 函数中

:。然后添加以下函数:

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [self.searchBarName resignFirstResponder];
}

同样如上所述,确保在标头中添加/扩展该函数
UISearchBarDelegate

In the header file, add or connect to the UISearchBar outlet: should look something like this:

@property (weak, nonatomic) IBOutlet UISearchBar *searchBarName;

Then, in the implementor, add the following:

self.searchBarName.delegate = self;

in the viewDidLoad function.

Then add the following function:

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [self.searchBarName resignFirstResponder];
}

Also as mentioned above ensure that this is added/extended in the header
UISearchBarDelegate

白首有我共你 2024-12-08 18:34:02

使用 [textField endEditing:YES];,它应该适合您,或者您可以创建一个按钮对象并执行 [self.buttonObj resignFirstResponder]

use [textField endEditing:YES];, it should work for you, or you can create an oject of ur button and do [self.buttonObj resignFirstResponder]

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