views:

1544

answers:

4

Hey guys,

I've got a UISearchBar in my interface and I want to customise the behaviour of the the small clear button that appears in the search bar after some text has been entered (it's a small grey circle with a cross in it, appears on the right side of the search field).

Basically, I want it to not only clear the text of the search bar (which is the default implementation) but to also clear some other stuff from my interface, but calling one of my own methods.

I can't find anything in the docs for the UISearchBar class or the UISearchBarDelegate protocol - it doesn't look like you can directly get access to this behaviour.

The one thing I did note was that the docs explained that the delegate method:

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText;

is called after the clear button is tapped.

I initially wrote some code in that method that checked the search bar's text property, and if it was empty, then it had been cleared and to do all my other stuff.

Two problems which this though:

Firstly, for some reason I cannot fathom, even though I tell the search bar to resignFirstResponder at the end of my method, something, somewhere is setting it back to becomeFirstResponder. Really annoying...

Secondly, if the user doesn't use the clear button, and simply deletes the text in the bar using the delete button on the keyboard, this method is fired off and their search results go away. Not good.

Any advice or pointers in the right direction would be great!

Thanks!

+1  A: 

I would suggest using the rightView and rightViewMode methods of UITextField to create your own clear button that uses the same image. I'm assuming of course that UISearchBar will let you access the UITextField within it. I think it will.
Be aware of this from the iPhone OS Reference Library:

If an overlay view overlaps the clear button, however, the clear button always takes precedence in receiving events. By default, the right overlay view does overlap the clear button.

So you'll probably also need to disable the original clear button.

Andrew
I'm pretty sure the UISearchBar doesn't expose it's texfield...
Jasarien
+1  A: 

I've got this code in my app. Difference is that I don't support 'live search', but instead start searching when the user touches the search button on the keyboard:

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
    if ([searchBar.text isEqualToString:@""]) {
        //Clear stuff here
    }
}
Rengers
+1 I agree with this answer. If the text is empty, after being cleared then it must mean that the clear button has been pressed.@Jasarien did you find a solution?
JoePasq
A: 

I think that the answer is a resounding "no"...

Jasarien
A: 

Hi,

The answer which was accepted is incorrect. This can be done, I just figured it out and posted it in another question:

http://stackoverflow.com/questions/1092246/uisearchbar-clearbutton-forces-the-keyboard-to-appear/3852509#3852509

Best

boliva