0

I want to place two Search display in one view controller(iPad). I drag two Search Display Controller in my view controller, however, only one search display works.

In the Connections Inspector, I find that one search display's outlet "searchDisplayController" is connected to "Search Display Controller", however the other one did not have this connection. I think that's why only one search display works.

My question is that: How can we use two search display in one view controller? I think my method: drag two Search Display Controller may be incorrect.

PS. I use the following code to determine which search display is focused.

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
    if (searchBar == self.customerTelSearchBar) {
        telSearchEditing = YES;
        addressSearchEditing = NO;
    }else if(searchBar == self.addressSearchBar){
        telSearchEditing = NO;
        addressSearchEditing = YES;
    }    
    return YES;
}
beaumelon
  • 11
  • 3

1 Answers1

1

Always do not work with storyboard, however when I use programmatic implementation of two Search display, it works. I post here my code:

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Init customerSearchDisplayController
    self.customerTelSearchBar.delegate = self;
    customerSearchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:self.customerTelSearchBar contentsController:self];
    customerSearchDisplayController.delegate = self;
    customerSearchDisplayController.searchResultsDataSource = self;
    customerSearchDisplayController.searchResultsDelegate = self;
    // Init addressSearchDisplayController
    self.addressSearchBar.delegate = self;
    addressSearchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:self.addressSearchBar contentsController:self];
    addressSearchDisplayController.delegate = self;
    addressSearchDisplayController.searchResultsDataSource = self;
    addressSearchDisplayController.searchResultsDelegate = self;
    // SearchBar status
    telSearchEditing = NO;
    addressSearchEditing = NO;
}

-(void)filterTelForSearchText:(NSString*)searchText {
    [filtredCustomersArray removeAllObjects];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.tel contains[c] %@",searchText];
    filtredCustomersArray = [NSMutableArray arrayWithArray:[allCustomersArray filteredArrayUsingPredicate:predicate]];
}

-(void)filterAddressForSearchText:(NSString*)searchText {
    [filtredAddressArray removeAllObjects];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.address contains[c] %@",searchText];
    filtredAddressArray = [NSMutableArray arrayWithArray:[allAddressArray filteredArrayUsingPredicate:predicate]];
}

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
    if (telSearchEditing) {
        [self filterTelForSearchText:searchString];
    }else if (addressSearchEditing){
        [self filterAddressForSearchText:searchString];
    }
        return YES;
}

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
    if (searchBar == self.customerTelSearchBar) {
        telSearchEditing = YES;
        addressSearchEditing = NO;
    }else if(searchBar == self.addressSearchBar){
        telSearchEditing = NO;
        addressSearchEditing = YES;
    }
    return YES;
}
beaumelon
  • 11
  • 3
  • Just adding some details... this was exactly what I needed and worked for me. I had different table layouts for portrait and landscape, thus 2 search bars. Maybe there was a way to share the search bar in the parent VC but I couldn't get that to work. And IB prevents you from connecting both bars to the one controller. If I understand what's going on in this code, by setting both controllers' delegates and datasources to self, you accomplish what IB is not able to construct. I see that UISearchDisplayController is deprecated in iOS8, so not sure what the modifications are. – Jack Bellis Mar 02 '15 at 20:58
  • The idea is to use the status of telSearchEditing and addressSearchEditing (bool) to tell delegate method 'shouldReloadTableForSearchString' which Search display is activated. The status of telSearchEditing and addressSearchEditing is defined in searchBarShouldBeginEditing. Hope helpful for you. – beaumelon Mar 04 '15 at 12:01