UISearchbar clearButton强制显示键盘

我有一个UISearchBar,它用作表视图的实时筛选器。 当通过endEditing:解除键盘时,查询文本和灰色圆形“清除”按钮保持不变。 从这里,如果我点击灰色的“清除”按钮,键盘会在文本被清除时重新出现。

我如何防止这种情况? 如果键盘当前未打开,我希望该按钮在不重新打开键盘的情况下清除文本。

有一种协议方法,当我点击清除按钮时会被调用。 但是发送UISearchBar resignFirstResponder消息对键盘没有任何影响。


这是一个古老的问题,我刚刚遇到了同样的问题,并设法通过以下方式解决它:

当由于用户点击“清除”按钮而调用UISearchBarDelegate的searchBar:textDidChange:方法时,searchBar尚未成为第一响应者,因此我们可以利用该方法来检测用户实际上何时旨在清除搜索并且不会将焦点带入搜索栏和/或执行其他操作。

为了跟踪这一点,我们需要在我们的viewController中声明一个BOOL ivar,它也是searchBar委托(我们称它为shouldBeginEditing ),并将其初始值设置为YES (假设我们的viewController类称为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:方法:

- (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;
}

基本上就是这样。

最好


我发现当触摸到“清除按钮”时调用了textDidChange时,resignFirstResponder不起作用。 但是,使用performSelection: withObject: afterDelay:似乎是一种有效的解决方法:

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    if ([searchText length] == 0) {
        [self performSelector:@selector(hideKeyboardWithSearchBar:) withObject:searchBar afterDelay:0];
    }
}

- (void)hideKeyboardWithSearchBar:(UISearchBar *)searchBar
{   
    [searchBar resignFirstResponder];   
}

我发现了一个非常安全的方法来知道是否按下了清除按钮,并忽略了用户刚刚删除UISearchBar的最后一个字符的时间。 这里是 :

- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    _isRemovingTextWithBackspace = ([searchBar.text stringByReplacingCharactersInRange:range withString:text].length == 0);

    return YES;
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    if (searchText.length == 0 && !_isRemovingTextWithBackspace)
    {
        NSLog(@"Has clicked on clear !");
    }
}

非常简单直接,是不是:)? 唯一需要注意的是,如果用户在编辑UISearchBar的UITextField时单击清除按钮,则将有两个ping,而如果用户在未编辑时单击它,则只会获得一个ping。


编辑:我无法测试它,但根据Rotem,这里是迅捷版本:

var isRemovingTextWithBackspace = false

func searchBar(searchBar: UISearchBar, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool
{
    self.isRemovingTextWithBackspace = (NSString(string: searchBar.text!).stringByReplacingCharactersInRange(range, withString: text).characters.count == 0)
    return true
}

func searchBar(searchBar: UISearchBar, textDidChange searchText: String)
{
    if searchText.characters.count == 0 && !isRemovingTextWithBackspace
    { 
        NSLog("Has clicked on clear !")
    }
}

@ Rotem的更新(Swift2):

var isRemovingTextWithBackspace = false

func searchBar(searchBar: UISearchBar, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
    self.isRemovingTextWithBackspace = (NSString(string: searchBar.text!).stringByReplacingCharactersInRange(range, withString: text).characters.count == 0)
    return true
}

func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
    if searchText.characters.count == 0 && !isRemovingTextWithBackspace {
        NSLog("Has clicked on clear!")
    }
}
链接地址: http://www.djcxy.com/p/44277.html

上一篇: UISearchbar clearButton forces the keyboard to appear

下一篇: UISearchBar Cancel Button