UISeachBar and delegate methods

ios, objective-c, uisearchbar, uitableview

Solution

You haven't set delegate to your searchBar object. After that line:

[searchBar setShowsScopeBar:YES];

add:

searchBar.delegate = self;

Don't forget to add `<UISearchBarDelegate>` protocol.

Problem

i'am trying to accomplish a search bar on a UITableView populated with a NSMutableArray. The problem is that the search bar delegate methods are obviously not working. When i type something in the search bar i just got no answer, even the NSLog(@"test") doesn't trigger, and that's driving me crazy. if i can get any help please. (Rookie ObjC level here ^^) Here is my code (minus all the parsing xml part) : (.h) ``` #import <UIKit/UIKit.h> #import <Foundation/Foundation.h> @interface MetamorphViewController : UITableViewController <UISearchBarDelegate, UISearchDisplayDelegate>{ IBOutlet UITableView * newsTable; UISearchBar *searchBar; UISearchDisplayController *searchDisplayController; UIActivityIndicatorView * activityIndicator; CGSize cellSize; NSXMLParser * rssParser; NSMutableArray * stories; NSArray * newStories; NSMutableArray *filteredStories; NSString *dataType; NSString *idName; BOOL *uploaded; BOOL isFiltered; NSMutableDictionary * item; NSString * currentElement; NSMutableString * currentTitle, * currentDate, * currentSummary, * currentLink, * currentModule, *currentOwner, *currentOwnername; } - (void)parseXMLFileAtURL:(NSString *)URL typeOfModule:(NSString *)typeOfData; @end ``` (.m) ``` - (void)viewDidLoad { self.navigationItem.rightBarButtonItem = self.editButtonItem; searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0,70,320,44)]; [searchBar setShowsScopeBar:YES]; [searchBar setScopeButtonTitles:[[NSArray alloc] initWithObjects:@"OBJ",@"C", nil]]; searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self]; searchDisplayController.delegate = self; searchDisplayController.searchResultsDataSource = self; [searchDisplayController setSearchResultsDelegate:self]; self.tableView.tableHeaderView = searchBar; [self.tableView reloadData]; self.tableView.scrollEnabled = YES; } -(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{ NSLog(@"test"); //NSPredicate *filterPredicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", searchBar]; //newStories = [stories filteredArrayUsingPredicate:filterPredicate]; //NSLog(@"newStories %@", newStories); } ```

Original source