How to save scope even after clicking "Cancel"?

I have a UITableView with a search bar at the top. I used the UISearchDisplayController to implement it. It also has a panel with two buttons. By default, when the application starts, the visibility bar is displayed. When I click the Cancel button after searching, the visibility bar has disappeared. So is there a way to keep the scope even after clicking the Cancel button. I used the following code but did not work.

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar
{
    [searchBar setShowsScopeBar:YES];
    return YES;
}

Thanks:)

+3
source share
3 answers

, , .

:

  • 'sizeToFit' showScopeBar. , searchBars , .
  • , , , , SearchBar . , tableHeaderView searchBar (), , , .

:

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar
{
    self.searchBar.showsScopeBar = YES;
    [self.searchBar sizeToFit];
    self.tableView.tableHeaderView = self.searchBar;
    return YES;
}
+5

UISearchBarController , , - UISearchBar - UISearchDisplayController !

UISearchBar UISearchBarDelegate , , , .

. - - , - , - , . - ,

// Filters the table when requested
- (void)filterContentForSearchBar:(UISearchBar *)searchBar
{
    NSString *scope = [[searchBar scopeButtonTitles] objectAtIndex:[searchBar selectedScopeButtonIndex]];
    NSString *search = [searchBar text];

    NSMutableArray *predicates = [[NSMutableArray alloc] init];

    if ([scope isEqualToString:@"Selected"])
    {
        [predicates addObject:[NSPredicate predicateWithFormat:@"selected == 1"]];
    }

    if (search && search.length) {
        [predicates addObject:[NSPredicate predicateWithFormat:@"name contains[cd] %@", search]];
    }

    NSPredicate *predicate = [NSCompoundPredicate andPredicateWithSubpredicates:predicates];

    self.filteredObjectList = [self.objectList filteredArrayUsingPredicate:predicate];

    [self.myTableView reloadData];
}


#pragma mark - UISearchBarDelegate Methods

// React to any delegate method we are interested in and change whatever needs changing
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    searchBar.showsCancelButton = true;
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    searchBar.showsCancelButton = false;
    [searchBar resignFirstResponder];

    searchBar.text = nil;
    [self filterContentForSearchBar:searchBar];
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    searchBar.showsCancelButton = false;
    [searchBar resignFirstResponder];
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    [self filterContentForSearchBar:searchBar];
}

- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope
{
    [self filterContentForSearchBar:searchBar];
}

:)

+4

answer :

@property (nonatomic) BOOL shouldHideFirstResponder; //assign YES in init

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {
    if (self.shouldHideFirstResponder) {
        self.searchBar.showsScopeBar = YES;
        [self.searchBar sizeToFit];
        self.table.tableHeaderView = self.searchBar;
    }

    return self.shouldHideFirstResponder;
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    self.shouldHideFirstResponder = NO;
}

- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar {
    self.shouldHideFirstResponder = YES;
}

- (void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller {
    if (!self.searchBar.text || self.searchBar.text.length < 1) {
        self.shouldHideFirstResponder = YES;
    }
}
0

Source: https://habr.com/ru/post/1617222/


All Articles