简体   繁体   English

如何取消UISearchbar中的键盘?

[英]How to cancel the keyboard in UISearchbar?

I'm using UISearchBar in my application and it serves as both an edit field as well as a search. 我在我的应用程序中使用UISearchBar ,它既可以作为编辑字段,也可以作为搜索。 So when I want to disappear the keyboard I have to use cancel button in UISearchBar but I can't have that cancel button on screen, so how could T make keyboard disappear when not used without using cancel button. 因此,当我想要消失键盘时,我必须在UISearchBar中使用取消按钮,但我不能在屏幕上有取消按钮,那么如果不使用取消按钮不使用T键盘会如何消失。 Please help me as i'm new to iPhone application development. 请帮助我,因为我是iPhone应用程序开发的新手。

Thanks in advance! 提前致谢!

Use this: 用这个:


[UISearchBar resignFirstResponder];

Just replace the word UISearchBar with the name of the object you have created. 只需将UISearchBar一词替换为您创建的对象的名称即可。

Are you looking for ways you can dismiss the keyboard or how to actually do that programmatically? 您是否正在寻找可以解除键盘或如何以编程方式实际执行此操作的方法? If programmatically, then [UISearchBar resignFirstResponder] . 如果以编程方式,则[UISearchBar resignFirstResponder] If you are looking for a possible way for the user to achieve that you can either make the return button on the keyboard resign its first responder status when pressed, or attach a UIGestureRecognizer to your view and set it up so that when the user clicks outside the keyboard, this keyboard goes away. 如果您正在寻找一种可供用户实现的方法,您可以让键盘上的返回按钮在按下时重新设置其第一响应者状态,或者将UIGestureRecognizer附加到您的视图并进行设置,以便在用户点击外部时键盘,这个键盘消失了。

simply all you need to do is to get UITextfield control from the UISearchbar and then set UITextfield's delegate to whatever delegate that will perform -(void) textFieldShouldReturn:(UITextField *)textField 您只需要从UISearchbar获取UITextfield控件,然后将UITextfield的委托设置为将执行的任何委托-(void) textFieldShouldReturn:(UITextField *)textField

-(void)viewDidLoad{    
    UIView * subView;
    NSArray * subViews = [searchbar subviews];
    for(subView in subViews)
    {
        if( [subView isKindOfClass:[UITextField class]] )
        {
           ((UITextField*)subView).delegate=self;
            ((UITextField*)subView).returnKeyType=UIReturnKeyDone;
            break;
        }
    }
}

-(BOOL) textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return TRUE;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM