如何在UISearchDisplayController的UISearchBar中更改"取消"按钮,"无结果"标签的字符串?

Cha*_*gUZ 10 iphone uisearchbar uisearchdisplaycontroller ios

如何在UISearchDisplayController的UISearchBar中更改"取消"按钮,"无结果"标签的字符串?

Cha*_*gUZ 13

我自己解决了.

取消按钮>

(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];
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

没有结果文本>

- (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];
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我使用计时器和bool值.如果没有计时器,则在"无结果"显示时无法更改文本.

- (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];
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 5

要更改"无结果"文本,您可以使用:

[self.searchDisplayController setValue:@"my no result text"  forKey: @"noResultsMessage"];
Run Code Online (Sandbox Code Playgroud)

我刚刚在iOS8中测试过