If you are writing a iOS app that contains of UITableView along with a UISearchBar, you might want to change the title of the cancel button, i.e. if you’re localizing your app.

But the cancel button isn’t that trivial to get a hold of.
Here is some code that allows you to change the title:
Implement the

searchDisplayControllerWillBeginSearch

method and have it contain the following:

- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller{
   self.searchDisplayController.searchBar.showsCancelButton = YES;
   UIButton *cancelButton = nil;
   for (UIView *subView in self.searchDisplayController.searchBar.subviews) {
      if ([subView isKindOfClass:NSClassFromString(@"UIButton")]) {
         cancelButton = (UIButton*)subView;
      }
   }
   [cancelButton setTitle:@"Annuller" forState:UIControlStateNormal];
}

The above code assumes that your class is a

UISearchDisplayDelegate

About the Author: Jesper Nielsen

IT developer situated in Silkeborg, Denmark. Working primarily with Java, and web technologies. Got a profound passion for mobile development, on the iOS platform.