2011-08-30 11 views

Respuesta

13

Lo resolví yo mismo.

botón Cancelar>

(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 hay resultados de texto>

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

utilizo temporizador y bool valor. Si no hay temporizador, no se puede cambiar el texto cuando primero se muestra "Sin resultados".

- (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

¿Hay alguna forma de hacerlo sin buscar las subvistas en iOS 5.0 y Arc? – inforeqd

+0

Quizás esto no sea necesario en iOS5 + – ChangUZ

+0

Escribí un artículo relevante sobre este tipo de problemas: http://artsy.github.com/blog/2012/05/11/on-making-it-personal--in-iOS -with-searchbars/ – orta

4

Gracias ChangUZ por encontrar la manera. Ahora, para mejorar, no se necesita un temporizador para alterar la etiqueta "Sin resultados".

- (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

Con el fin de cambiar el texto "ningún resultado" que puede utilizar:

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

yo sólo he probado en iOS8

+0

Probado en iOS7 e iOS8. Funciona como un encanto. – tounaobun

+0

funcionó bien en iOS 9.1 – drshock

0

Una solución más sencilla para cambiar el texto botón Cancelar:

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

probado en iOS 10

Cuestiones relacionadas