ios 如何使用 UIsearchBar Delegate 方法关闭键盘?我尝试了所有方法,但没有用。
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15386488/
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
How to dismiss the Keyboard using UIsearchBar Delegate methods? i tried every method but no use.
提问by Logunath
Hi i am using a UIsearchBar in my app, i tried all methods in UISearchBar Delegate to dismiss the keyboard but no use. could anyone help me out.
嗨,我在我的应用程序中使用了 UIsearchBar,我尝试了 UISearchBar Delegate 中的所有方法来关闭键盘,但没有用。谁能帮帮我。
some codes i tried
我试过的一些代码
1.
1.
-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
[searchBar resignFirstResponder];
}
2.
2.
-(BOOL) searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if([text isEqualToString:@"\n"])
{
[searchBar resignFirstResponder];
return NO;
}
return YES;
}
3.
3.
- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar
{
[searchBar resignFirstResponder];
}
回答by nsgulliver
First of all you should implement UISearchBarDelegate
in your class & check if you have connected the to your UISearchBar
or not?
首先,您应该UISearchBarDelegate
在您的班级中实施并检查您是否已将其连接到您的课程UISearchBar
?
If you are adding UISearchBar
through IB then you should select delegate property of the UISearchBar to the file's owner. You should make sure that you are connecting IBOutlet
for the UISearchBar
in the code properly .
如果您UISearchBar
通过 IB添加,那么您应该选择 UISearchBar 的委托属性给文件的所有者。你应该确保你所连接 IBOutlet
的UISearchBar
代码正确。
If you are adding UISearchBar
via code then you should set the delegate yourself in the code like this searchBar.delegate=self;
如果您UISearchBar
通过代码添加,那么您应该像这样在代码中自己设置委托searchBar.delegate=self;
Next step is to make sure that delegate methods are being called, which is obvious if you have connected delegate properly, best thing to check stuff is putting break pointer in your code or print through NSLog
.
下一步是确保调用委托方法,如果您已正确连接委托,这很明显,检查内容的最佳方法是在代码中放置中断指针或通过NSLog
.
Finally use this method to hide the keyboard if you want to hide when user clicks to search
最后,如果您想在用户单击搜索时隐藏键盘,请使用此方法隐藏键盘
- (void) searchBarSearchButtonClicked:(UISearchBar *)searchBar {
[searchBar resignFirstResponder];
}
OR you could try when editing finished
或者你可以在编辑完成后尝试
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
[searchBar resignFirstResponder];
}
回答by Balu
Try this:
尝试这个:
Set the delegate on the search bar and check whether the IBOutlet is connected or not
在搜索栏设置delegate,检查IBOutlet是否连接
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
[searchBar resignFirstResponder];
}