Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change strings of "Cancel" button, "No Results" label in UISearchBar of UISearchDisplayController?

How can I change strings of "Cancel" button, "No Results" label in UISearchBar of UISearchDisplayController?

like image 319
ChangUZ Avatar asked Aug 30 '11 05:08

ChangUZ


2 Answers

I solved it myself.

Cancel Button>

(void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller {
    [controller.searchBar setShowsCancelButton:YES animated:NO];
    for (UIView *subview in [controller.searchBar subviews]) {
        if ([subview isKindOfClass:[UIButton class]]) {
            [(UIButton *)subview setTitle:@"_____" forState:UIControlStateNormal];
        }
    }
}

No Results Text>

- (void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView {
    if (!isChangedNoResults) {
        if ([contactManager.filteredPeople count] == 0) {
            [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(changeNoResultsTextToKorean:) userInfo:nil repeats:YES];
        }
    }
}

I use timer and bool value. If no timer, can not change text when "No Results" show first.

- (void)changeNoResultsTextToKorean:(NSTimer *)timer {
    if (isChangedNoResults) {
        [timer invalidate];
    }
    else {
        for (UIView *subview in [self.searchDisplayController.searchResultsTableView subviews]) {
            if ([subview isKindOfClass:[UILabel class]]) {
                UILabel *targetLabel = (UILabel *)subview;
                if ([targetLabel.text isEqualToString:@"No Results"]) {
                    NSLog(@"Changed!");
                    [targetLabel setText:@"_____"];
                    isChangedNoResults = YES;
                    [timer invalidate];
                }
            }
        }
    }
}
like image 77
ChangUZ Avatar answered Oct 26 '22 21:10

ChangUZ


In order to change the "no result" text you can use :

[self.searchDisplayController setValue:@"my no result text"  forKey: @"noResultsMessage"];

I've just tested in iOS8

like image 38
Mida Boghetich Avatar answered Oct 26 '22 22:10

Mida Boghetich