ios 按下回车键时,UISearchBar 不会关闭键盘
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12636708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The UISearchBar doesn't dismiss the keyboard when enter is pressed
提问by aneuryzm
The UISearchBar
doesn't dismiss the keyboard when enter is pressed, or the user touch somewhere else.
在UISearchBar
不解雇时输入按下键盘或触摸用户别的地方。
I need to use the remove keyboard button on the bottom right of the iOS keyboard in order to remove the keyboard and invoke:
我需要使用 iOS 键盘右下角的移除键盘按钮来移除键盘并调用:
- (void)searchBarTextDidEndEditing:(UISearchBar *)aSearchBar
How can I fix it?
我该如何解决?
采纳答案by Kamleshwar
- (void)searchBarTextDidEndEditing:(UISearchBar *)aSearchBar {
[aSearchBar resignFirstResponder];
}
Also you have to set delegate for the UISearchBar: UISearchBarDelegate
您还必须为 UISearchBar 设置委托:UISearchBarDelegate
It should work.
它应该工作。
Another option is searchBarSearchButtonClicked we can use.
另一种选择是 searchBarSearchButtonClicked 我们可以使用。
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[searchBar resignFirstResponder];
// You can write search code Here
}
回答by Paresh Navadiya
Add UISearchBarDelegatein .h
在 .h 中添加UISearchBarDelegate
Also set SearchBar's object delegate to self.
还设置搜索栏的对象委托自我。
you should do add UISearchBarDelegate
's method:
你应该做 addUISearchBarDelegate
的方法:
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[searchBar resignFirstResponder];
// Do the search...
}
EDIT :Above doesnot work then add this:
编辑:以上不起作用然后添加:
[self.view endEditing:YES];
回答by Bharath
Use the following code snippet to close/hide the keyboard when return button is clicked.
使用以下代码片段在单击返回按钮时关闭/隐藏键盘。
- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if([text isEqualToString:@"\n"])
{
[searchBar resignFirstResponder];
return NO;
}
return YES;
}
回答by Alsh compiler
for swift 1.2 keyboard will hide when you click finished , and there is another function for cancel but it's not good to use it as when the user click cancel he might want to search for another word ...
对于 swift 1.2 键盘将在您单击完成时隐藏,并且还有另一个取消功能,但使用它并不好,因为当用户单击取消时,他可能想搜索另一个词...
func searchBarSearchButtonClicked(searchBar: UISearchBar) {
searchBar.resignFirstResponder()
}
回答by Abhishek Mishra
it will work -
它会起作用-
objective c -
目标 c -
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
[searchBar resignFirstResponder];
}
Swift -
斯威夫特 -
func searchBarSearchButtonClicked(searchBar: UISearchBar) {
searchBar.resignFirstResponder()
}