xcode 单击 X 按钮时重置 UISearchBar 文本字段中的文本,但不退出第一响应者
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7447248/
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
Reset text in textfield of UISearchBar when X button is clicked, but not resign firstresponder
提问by Magnus
I have a view with a UISearchBar in it. However, when the "X" button in the searchbar is clicked, it removes the keyboard but doesn't remove the text in the textfield of the searchbar. What I want is this: When the user clicks the x button, the text in the textfield is reset but the keyboard stays up.
我有一个带有 UISearchBar 的视图。但是,当单击搜索栏中的“X”按钮时,它会删除键盘,但不会删除搜索栏文本字段中的文本。我想要的是:当用户单击 x 按钮时,文本字段中的文本被重置但键盘保持不动。
How can I do this?
我怎样才能做到这一点?
回答by flizit
This seems to do the trick for me. Make sure your View Controller is a UISearchBarDelegate
这似乎对我有用。确保您的视图控制器是 UISearchBarDelegate
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
searchBar.text = @"";
}
回答by Harald
Just set the text to nilin searchBarCancelButtonClickeddelegate method :
只需在searchBarCancelButtonClicked委托方法中将文本设置为nil:
searchBar.text = nil;
回答by Mouhamad Lamaa
to clear text use this searchBar.text = @"";
清除文本使用这个 searchBar.text = @"";
回答by Nick
try implementing UISearchBarDelegate
's method -(BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar
and return NO
as the result.
尝试实现UISearchBarDelegate
的方法-(BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar
并NO
作为结果返回。
E.g.:
例如:
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar
{
return NO;
}