【发布时间】:2011-08-30 05:00:04
【问题描述】:
如何更改“取消”按钮、“无结果”标签的字符串 在 UISearchDisplayController 的 UISearchBar 中?
【问题讨论】:
标签: ios iphone uisearchbar uisearchdisplaycontroller
如何更改“取消”按钮、“无结果”标签的字符串 在 UISearchDisplayController 的 UISearchBar 中?
【问题讨论】:
标签: ios iphone uisearchbar uisearchdisplaycontroller
我自己解决了。
取消按钮>
(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];
}
}
}
}
}
【讨论】:
didLoadSearchResultsTableView: 替换为willLoadSearchResultsTableView: 以在No Results 文本出现之前对其进行修改,但这不起作用。
为了更改“无结果”文本,您可以使用:
[self.searchDisplayController setValue:@"my no result text" forKey: @"noResultsMessage"];
我刚刚在 iOS8 中测试过
【讨论】:
感谢 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;
}
【讨论】:
更改取消按钮文本的更简单的解决方案:
[self.searchDisplayController.searchBar setValue:@"custom text" forKey:@"cancelButtonText"];
在 iOS 10 中测试
【讨论】: