Daniel Gorelik
Daniel Gorelik

Reputation: 289

How can external UITableView delegate hide the UISearchBar keyboard?

I have a table view with a data source/delegate in another file. In addition, there is a search bar above the table view that belongs to the first file. In other to hide the keyboard when scrolling, I would need to call:

[self.searchBar resignFirstResponder]

But the

(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView

method is in the delegate. So how would I hide the keyboard when scrolling in this case?

Thanks!

Upvotes: 1

Views: 415

Answers (2)

Naresh Reddy M
Naresh Reddy M

Reputation: 1096

There are many ways to do,a couple of them are below.

option 1: add below line after initializing your table object

[yourTableView setKeyboardDismissMode:UIScrollViewKeyboardDismissModeOnDrag];

or

option 2: Get your tableview's superview(i'm expecting that as aViewcontrollerObj.view) and forcibly end its editing .

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    UIView *tableviewSuperView = yourTableView.superview;
    [tableviewSuperView endEditing:true];
}

Hope that helps Happy coding :)

Upvotes: 0

vaskal08
vaskal08

Reputation: 80

you could send a notification in scrollviewwillbegindragging. tableview delegate:

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
     [[NSNotificationCenter defaultCenter] postNotificationName:@"resign" object:nil];
}

searchbar delegate:

-(void)viewDidLoad{
     [super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(goTo:) name:@"resign" object:nil];
}

-(void)goTo:(NSNotification*)notification {
[self.searchBar resignFirstResponder];

}

Upvotes: 1

Related Questions