0

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!

xcodenab
  • 43
  • 2
  • 11

1 Answers1

0
  1. If you don't use storyboards/nibs you don't need the IBOutlet keyword in the property declaration
  2. You don't assign self as the delegate of your UISearchBar therefor the delegate methods won't be called
  3. -(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *) is a method of the UISearchDisplayDelegate protocol, but you didn't mention that you are using a UISearchDisplayController. Without that this delegate method will never be called. The delegate you need to implement for an UISearchBar without display controller is UISearchBarDelegate.
Cornelius
  • 4,214
  • 3
  • 36
  • 55
  • Wow thanks a lot that helped me out! How do I use the UISearchDisplayController? And now if I tap on the screen the App crashes saying ::::::::: 014-06-26 12:12:35.995 SAPRetailEx_SAP[28898:60b] -[UITapGestureRecognizer resignFirstResponder]: unrecognized selector sent to instance 0x151b0500 – xcodenab Jun 26 '14 at 10:14
  • This is a great answer on how to use the `UISearchDisplayController`: http://stackoverflow.com/a/18719797/396578 The crash says that you try to call `resignFirstResponder` on an instance of `UITapGestureRecognizer` instead of a `UITextField`) ... Best set a breakpoint there and inspect the object that is used. – Cornelius Jun 26 '14 at 10:34
  • Okay I will, thank you very much for your effort! ;) – xcodenab Jun 26 '14 at 10:49