Q:

Programmatically create a UISearchBar that can be used from multiple views

I would like to create a single UISearchBar that can be viewed and used from multiple views in my app.
For this reason, I have declared a UISearchBar property in my AppDelegate class and initialise it in my very first UIViewController. 
@interface AppDelegate : UIResponder <UIApplicationDelegate>{

    IBOutlet UIWindow *window;

}

@property (strong, nonatomic) IBOutlet UISearchBar *searchBar;

my first view .h
@interface MainTableViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, UISearchDisplayDelegate, UISearchBarDelegate>{

    AppDelegate *appDelegate;
    NSArray *searchResults;
}

@property (strong, nonatomic) IBOutlet UIBarButtonItem *accountPage;
@property (strong, nonatomic) IBOutlet UIBarButtonItem *search;

my first view .m
    - (void)viewDidLoad
    {
        [super viewDidLoad];

        appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

        self.search = [self.search initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(showSearchBar:)];

    }

        - (IBAction)showSearchBar:(id)sender{

            UIBarButtonItem *searchItem = (UIBarButtonItem *) sender;

            if (searchItem.tag == 0) {
                if (!appDelegate.searchBar) {
                    appDelegate.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0,0,320,44)];
                    [[UISearchDisplayController alloc] initWithSearchBar:appDelegate.searchBar contentsController:self];
                    self.searchDisplayController.searchResultsDelegate = self;
                    self.searchDisplayController.searchResultsDataSource = self;
                    self.searchDisplayController.delegate = self;
                    [self.view addSubview:appDelegate.searchBar];
                } else{
                    [appDelegate.searchBar setHidden:NO];
                }
                searchItem.tag = 1;
            } else{
                searchItem.tag = 0;
                [appDelegate.searchBar setHidden:YES];
            }
        }

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        return [searchResults count];

    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    if (tableView == self.searchDisplayController.searchResultsTableView) {
        cell.textLabel.text = [searchResults objectAtIndex:indexPath.row];
    } 
    // Set up the cell
    return cell;
}

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
    NSLog(@"filterContentForSearchText");
    NSPredicate *resultPredicate = [NSPredicate
                                    predicateWithFormat:@"(name contains[cd] %@)",
                                    searchText];

    searchResults = [appDelegate.elements filteredArrayUsingPredicate:resultPredicate];
}

- (BOOL) searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString{

    [self filterContentForSearchText:searchString
                               scope:[[appDelegate.searchBar scopeButtonTitles]
                                      objectAtIndex:[appDelegate.searchBar
                                                     selectedScopeButtonIndex]]];

    return YES;
}

However, while the search bar initialised and displayed fine on the VC the search mechanism is not working. I am able to write in search text box but no action. Even 'Search' button from keyboard is not working.

A:

Use Search Bar and Search Display Controller object in XIB. Simply drag and drop this object in the XIB
and implement table view delegate and data source method.
Like
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(tableView == self.aTableView)
    return [self.categories count];//normal table view
    return [self.searchCategories count];//search display controllers TableView
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"reusableCells";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if(cell == nil)
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault     reuseIdentifier:identifier] autorelease];
    NSString *text = nil;
    if(tableView == self.aTableView)
        text = [self.categories objectAtIndex:indexPath.row];
    else
       text = [self.searchCategories objectAtIndex:indexPath.row];

    cell.textLabel.text = text;

    return cell;
}

Search Display controller Methods
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString{
    if(self.searchCategories == nil){
        NSMutableArray *arr = [[NSMutableArray alloc] init];
        self.searchCategories = arr;
        [arr release];
    }else{
        [self.searchCategories removeAllObjects];
    }

    for (NSString *aStr in self.categories) {
        if([[aStr lowercaseString] rangeOfString:[searchString lowercaseString]].location != NSNotFound){
           [self.searchCategories addObject:aStr];
        }
    }
    [controller.searchResultsTableView reloadData];
    return NO;
}

- (void) searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller{
    [self.aTableView setHidden:YES];
}
- (void) searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller{
    [self.aTableView setHidden:NO];
}

Hope This will help you. if still you have any problem i recommend you to read details of    
UISearchDisplayController

