【问题标题】:How can I change strings of "Cancel" button, "No Results" label in UISearchBar of UISearchDisplayController?如何在 UISearchDisplayController 的 UISearchBar 中更改“取消”按钮、“无结果”标签的字符串?
【发布时间】:2011-08-30 05:00:04
【问题描述】:

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

【问题讨论】:

    标签: ios iphone uisearchbar uisearchdisplaycontroller


    【解决方案1】:

    我自己解决了。

    取消按钮>

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

    没有结果文本>

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

    我使用计时器和布尔值。 如果没有计时器,则不能在“无结果”首先显示时更改文本。

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

    【讨论】:

    • 在不搜索 iOS 5.0 和 Arc 中的子视图的情况下是否可以这样做?
    • iOS5+可能不需要这个
    • 我写了一篇关于这类问题的相关文章:artsy.github.com/blog/2012/05/11/…
    • 有人能解释一下为什么这里需要计时器吗?我很好奇将didLoadSearchResultsTableView: 替换为willLoadSearchResultsTableView: 以在No Results 文本出现之前对其进行修改,但这不起作用。
    • 这段代码是2年前写的。现在,我开发的是 Android 而不是 iOS。所以我不记得确切...对不起
    【解决方案2】:

    为了更改“无结果”文本,您可以使用:

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

    我刚刚在 iOS8 中测试过

    【讨论】:

    • 在 iOS7 和 iOS8 中测试过。像魅力一样工作。
    【解决方案3】:

    感谢 ChangUZ 找到方法。现在,为了改进,不需要计时器来更改“无结果”标签。

    - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
    {
        dispatch_async(dispatch_get_main_queue(), ^(void) {
            for (UIView *v in controller.searchResultsTableView.subviews) {
                if ([v isKindOfClass:[UILabel self]]) {
                    ((UILabel *)v).text = @"_____";
                    break;
                }
            }
        });
        return YES;
    }
    

    【讨论】:

      【解决方案4】:

      更改取消按钮文本的更简单的解决方案:

      [self.searchDisplayController.searchBar setValue:@"custom text" forKey:@"cancelButtonText"];
      

      在 iOS 10 中测试

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多