Search code examples
iosipadsearchbarsearchdisplaycontroller

How to create TWO Search Display IPAD


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;
}

Solution

  • 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;
    }