2011-08-30 29 views

Trả lời

13

Tôi đã tự giải quyết.

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

Không Kết quả 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]; 
     } 
    } 
} 

tôi sử dụng hẹn giờ và bool giá trị. Nếu không có bộ hẹn giờ, không thể thay đổi văn bản khi "Không có kết quả" hiển thị trước.

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

là có anyway để làm điều này mà không cần tìm kiếm các subviews trong iOS 5.0 và Arc? – inforeqd

+0

Có thể điều này không cần thiết trong iOS5 + – ChangUZ

+0

Tôi đã viết bài viết có liên quan về các loại vấn đề sau: http://artsy.github.com/blog/2012/05/11/on-making-it-personal--in-iOS -with-searchbars/ – orta

4

Cảm ơn ChangUZ vì đã tìm đường. Bây giờ, để cải thiện, một bộ đếm thời gian là không cần thiết để thay đổi nhãn "Không có kết quả".

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

Để thay đổi "không có kết quả" văn bản mà bạn có thể sử dụng:

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

Tôi vừa mới thử nghiệm trong iOS8

+0

Được thử nghiệm trong iOS7 và iOS8.Đồng thời như một sự quyến rũ. – tounaobun

+0

hoạt động tốt trong iOS 9.1 – drshock

0

Một giải pháp đơn giản cho việc thay đổi các nút văn bản Cancel:

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

Đã kiểm tra trong iOS 10

Các vấn đề liên quan