nonmll nonmll - 1 month ago 10
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?

Thanks!

Answer

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];

}