I'm trying to setup a search bar for a table view in an iOS App. Table View is set up, so is the search bar. I CANNOT use an XIB/Storyboard file - unfortunately all tutorials are with one of them... Well the problem now is, that I can't use any UISearchBar functions such as textDidBeginEditing and so on.
.h File Declaration of the SearchBar (I loaded the UISearchDisplayDelegate in the .h file)
@property (nonatomic,strong) UISearchBar IBOutlet *searchBar;
Initialization of the SearchSearchBar in the ViewDidLoad of the .m file:
self.searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
[self.view addSubview:self.searchBar];
//DIsmiss Keyboard
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
The missing part of the dismissKeyboard method (works):
- (void)dismissKeyboard{
//Keyboard Hiding
[_searchBar resignFirstResponder];
}
Those are the two other methods I got (for searching and reloading the view) which I used in a testing project and they worked there:
-(void) filterContentForSearchText:(NSString *)searchText scope:(NSString *)scope {
NSLog(@"Search Clicked");
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@", searchText];
self.searchResults = [self.array filteredArrayUsingPredicate:predicate];
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)
searchString {
[self filterContentForSearchText:searchString scope:[[self.searchDisplayController.searchBar scopeButtonTitles]objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
return YES;
}
I'm getting really desperate because all methods that use textEdit or something (which you obviously need in order to search) do not work and I can't figure out why, please help me!