views:

3404

answers:

3

Hi,

I'm not sure why this is so hard. I have a UISearchBar at the top of my page. When the user types into that UISearchBar, it will automatically filter the results in the UITableView that is below the search bar. There is no need for a Search button since I search when keys are pressed.

The flow of the application is that the user types a search in the search bar (with the keyboard being displayed) and then will scroll the results in the table view - at which point the keyboard needs to disappear.

I'm having a hard time getting the keyboard to disappear.

I know I need to do:

    [searchBar resignFirstResponder];

to get the keyboard to disappear but I can't find what delegate I need to perform this on. I want to do this as soon as the user touches the table view.

Any ideas?

+1  A: 

You might want to do it as Cydia (jailbroken packaging UI) does it - there is a search button, and when you press the search button, it closes the keyboard. The results are still filtered as you type for a preview.

Isaac Waller
This would necessitate the pressing of the search button to remove the keyboard - a step that I feel should be unnecessary and might just frustrate users. Thanks though.
rein
+2  A: 

I accomplished this using the sample code from this tutorial

Additionally, it provides a nice dimmed overlay over the tableview while in the search box. when you touch the tableview, the dimming goes away and the keyboard slides down.

Corey Floyd
Thanks - this is perfect. A little sneaky, and the effect is nice.
rein
If you want the dimming effect but don't care to block the touch input, you can do it by reducing the alpha of the bit you want to dim, assuming you have a dark gray or black background behind it; then just set alpha = 1.0 when you exit the mode. It's a lot less code, too.
Amagrammer
I managed to achieve the same effect (translucent overlay for the dimming effect, cancels search when touched up inside) by overlaying a custom button over the tableView. Since I was creating and adding the button subview programmatically, the hardest part was figuring out how to detect `touchUpInside` events programmatically (the answer is to use `-addTarget:action:forControlEvents:`).
Chu Yeow
Thanks Chu that's great tip.
Simo Salminen
+2  A: 

Try this:

- (void)viewWillDisappear:(BOOL)animated {

    [super viewWillDisappear:animated];

    [searchBar resignFirstResponder];

}

It worked for me

H Vargas