UISearchbar clearButton forces the keyboard to appear(UISearchbar clearButton 强制键盘出现)
问题描述
我有一个 UISearchBar 作为表格视图的实时过滤器.当通过 endEditing: 关闭键盘时,查询文本和灰色圆形清除"按钮仍然存在.从这里,如果我点击灰色的清除"按钮,键盘会在文本被清除时重新出现.
I have a UISearchBar which acts as a live filter for a table view. When the keyboard is dismissed via endEditing:, the query text and the gray circular "clear" button remain. From here, if I tap the gray "clear" button the keyboard reappears as the text is cleared.
如何防止这种情况发生?如果键盘当前未打开,我希望该按钮在不重新打开键盘的情况下清除文本.
How do I prevent this? If the keyboard is not currently open I want that button to clear the text without reopening the keyboard.
当我点击清除按钮时,会调用一个协议方法.但是向 UISearchBar 发送 resignFirstResponder 消息对键盘没有任何影响.
There is a protocol method that gets called when I tap the clear button. But sending the UISearchBar a resignFirstResponder message doesn't have any effect on the keyboard.
推荐答案
这是一个老问题,我刚刚遇到同样的问题,并设法通过以下方式解决它:
This is an old question and I just came across the same issue and managed to solve it the following way:
当 UISearchBarDelegate 的 searchBar:textDidChange:
方法因为用户点击清除"按钮而被调用时,searchBar 还没有成为第一响应者,所以我们可以利用为了检测用户何时实际上打算清除搜索并且不将焦点放在 searchBar 和/或做其他事情.
When the searchBar:textDidChange:
method of the UISearchBarDelegate gets called because of the user tapping the 'clear' button, the searchBar hasn't become the first responder yet, so we can take advantage of that in order to detect when the user in fact intended to clear the search and not bring focus to the searchBar and/or do something else.
为了跟踪它,我们需要在 viewController 中声明一个 BOOL
ivar,它也是 searchBar 委托(我们称之为 shouldBeginEditing
)并使用YES
的初始值(假设我们的viewController类叫做SearchViewController):
To keep track of that, we need to declare a BOOL
ivar in our viewController that is also the searchBar delegate (let's call it shouldBeginEditing
) and set it with an initial value of YES
(supposing our viewController class is called SearchViewController):
@interface SearchViewController : UIViewController <UISearchBarDelegate> {
// all of our ivar declarations go here...
BOOL shouldBeginEditing;
....
}
...
@end
@implementation SearchViewController
...
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
...
shouldBeginEditing = YES;
}
}
...
@end
稍后,在 UISearchBarDelegate 中,我们实现了 searchBar:textDidChange:
和 searchBarShouldBeginEditing:
方法:
Later on, in the UISearchBarDelegate, we implement the searchBar:textDidChange:
and searchBarShouldBeginEditing:
methods:
- (void)searchBar:(UISearchBar *)bar textDidChange:(NSString *)searchText {
NSLog(@"searchBar:textDidChange: isFirstResponder: %i", [self.searchBar isFirstResponder]);
if(![searchBar isFirstResponder]) {
// user tapped the 'clear' button
shouldBeginEditing = NO;
// do whatever I want to happen when the user clears the search...
}
}
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)bar {
// reset the shouldBeginEditing BOOL ivar to YES, but first take its value and use it to return it from the method call
BOOL boolToReturn = shouldBeginEditing;
shouldBeginEditing = YES;
return boolToReturn;
}
基本上就是这样.
最好的
这篇关于UISearchbar clearButton 强制键盘出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:UISearchbar clearButton 强制键盘出现
基础教程推荐
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01