nonmll nonmll - 1 year ago 107
iOS Question

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?


Answer Source

you could send a notification in scrollviewwillbegindragging. tableview delegate:

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

searchbar delegate:

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

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download