如何创建TWO搜索显示IPAD



我想在一个视图控制器(iPad)中放置两个搜索显示。我在视图控制器中拖动了两个搜索显示控制器,但是,只有一个搜索显示可以工作。

在连接检查器中,我发现一个搜索显示器的出口"searchDisplayController"连接到"search display Controller",但另一个没有这个连接。我认为这就是为什么只有一个搜索显示工作。

我的问题是:我们如何在一个视图控制器中使用两个搜索显示?我认为我的方法:拖动两个搜索显示控制器可能不正确。

PS。我使用以下代码来确定哪个搜索显示是焦点。

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

总是不使用故事板,但是当我使用两个搜索显示的编程实现时,它可以工作。我在这里张贴我的代码:

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

最新更新