开发者

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. 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 usin开发者_JS百科g cancel button. Please help me as i'm new to iPhone application development.

Thanks in advance!


Use this:


[UISearchBar resignFirstResponder];

Just replace the word UISearchBar with the name of the object you have created.


Are you looking for ways you can dismiss the keyboard or how to actually do that programmatically? If programmatically, then [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.


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

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜