点击X按钮时从UISearchBar中解除键盘 [英] Dismissing keyboard from UISearchBar when the X button is tapped

查看:131
本文介绍了点击X按钮时从UISearchBar中解除键盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用UISearchBar(但不是通常联合使用的SearchDisplayController),当你点击'X'按钮时我想解除键盘。

I'm using the UISearchBar (but not the SearchDisplayController that's typically used in conjunction) and I'd like to dismiss the keyboard when you hit the 'X' button.

我已经关注了 TomSwift的建议点击'X'时被调用,效果很好。但是,从文本字段中退出第一个响应者并在UISearchBar实例中调用(使用 resignFirstResponder )将不会使键盘消失。

I've followed TomSwift's suggestion on getting called when the 'X' is tapped and that works great. But resigning first responder from the text field and also invoking in the UISearchBar instance, both with resignFirstResponder, won't make the keyboard go away.

当用户轻敲X按钮时,有没有办法摆脱键盘?

Is there a way to get rid of the keyboard when the user has tapped the X button?

以下是我通过清除通知所做的工作:

Here's what I did to get the 'Clear' notify:

- (void)viewDidLoad:
{
    for (UIView* v in searchBar.subviews)
    {
        if ( [v isKindOfClass: [UITextField class]] )
        {
            UITextField *tf = (UITextField *)v;
            tf.delegate = self;
            break;
        }
    }    
}

然后我设置班级实现UISearchBarDelegate和UITextFieldDelegate。

Then I have my class setup to implement both UISearchBarDelegate and UITextFieldDelegate.

让该类充当textfield委托允许我接听此电话:

Having the class serve as the textfield delegate allows me to get this call:

- (BOOL)textFieldShouldClear:(UITextField *)textField
{
     [textField resignFirstResponder];
     [self.searchBar resignFirstResponder];
     return YES;
}

我已经尝试了我能想到的一切。我正在尝试的最后一件事是找到一种方法来发出UISearchDelegate将在我的Controller类上调用的'searchBarCancelButtonClicked',但不是我确定我怎么能这样做,因为UISearchBar似乎没有任何直接方法用这个名字调用。

I've tried everything I can think of. The last thing I'm trying is to find a way to issue the 'searchBarCancelButtonClicked' that UISearchDelegate will invoke on my Controller class but not I'm sure how I could do this since the UISearchBar doesn't seem to have any direct methods to invoke with this name.

推荐答案

更新:

嗯,这完全是黑客攻击,但我能够让它发挥作用。基本上代码调用取消按钮的处理程序。为了使它工作,我不得不延迟调用选择器,我不知道为什么这样做。另外,我必须为取消按钮编写一个访问器,就像你为文本字段所做的一样。

Well, this is a total hack but I was able to make it work. Basically the code invokes the handler for the cancel button. To make it work I had to invoke the selector with a delay, and I'm not sure why this had to be. Also, I had to write an accessor for the cancel button just like you did for the text field.

同样,这是一个完全黑客攻击。我不确定自己是否会在应用程序中自行执行此操作。

Again, this is a total hack. I'm not sure I'd do this myself in an app.

// this in the context of the search bar
- (UIButton*) cancelButton
{
    for (UIView* v in self.subviews)
    {
        if ( [v isKindOfClass: [UIButton class]] )
            return (UIButton*)v;
    }

    return nil;
}

// this is the textField delegate callback
- (BOOL)textFieldShouldClear:(UITextField *)textField
{
    [textField resignFirstResponder];

    UIButton* cb = _searchBar.cancelButton;

    NSObject* target = [[cb allTargets] anyObject];

    NSArray* actions = [cb actionsForTarget: target forControlEvent:UIControlEventTouchUpInside];

    NSString* selectorName = [actions  objectAtIndex:0];

    SEL selector = NSSelectorFromString( selectorName );

    [target performSelector: selector withObject: cb afterDelay: 0.1];

    return YES;
}

原始答案:

如何在第一时间显示清晰的X按钮?在我的测试用例中,我没有看到它显示...

How do you get the clear 'X' button to display in the first place? In my test case I dont see it displaying...

尝试在searchBar上执行resignFirstResponder,而不是textField。

Try doing a resignFirstResponder on the searchBar, not the textField.

这篇关于点击X按钮时从UISearchBar中解除键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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