UISearchbar clearButton强制键盘出现 [英] UISearchbar clearButton forces the keyboard to appear

查看:193
本文介绍了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:

searchBar:textDidChange:方法时由于用户点击清除按钮,UISearchBarDelegate被调用,搜索栏尚未成为第一个响应者,因此我们可以利用它来检测用户实际上何时打算清除搜索而不是专注于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.

为了跟踪这一点,我们需要声明一个 BOOL 我们的viewController中的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强制键盘出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆