1

I have a UISearchBar in my app, and I'm running the following code with it:

- (void)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
    [searchBar setShowsCancelButton:YES animated:YES];
}

So when the user clicks the search bar, a cancel button pops up next to it.

This, unfortunately, makes it so the text box isn't selected, and the keyboard doesn't pop up. You have to click the search bar a second time to get those things to happen.

How would I fix this?

Thanks.

theChrisKent
  • 15,029
  • 3
  • 61
  • 62
rottendevice
  • 2,849
  • 6
  • 30
  • 37

2 Answers2

1

The method should be like this:

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
    [searchBar setShowsCancelButton:YES animated:YES];
    return YES;
}

The key element is returning YES in this method to indicate that it should in fact begin editing.

Documentation here: http://developer.apple.com/library/ios/documentation/uikit/reference/UISearchBarDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/UISearchBarDelegate/searchBarShouldBeginEditing:

theChrisKent
  • 15,029
  • 3
  • 61
  • 62
0

could you change your code as below

 [searchBar setShowsCancelButton:NO animated:YES];

I think your were passing YES in the above method.

Jhaliya - Praveen Sharma
  • 31,697
  • 9
  • 72
  • 76