Iphone – sliding UISearchBar

cocoa-touchiphone

I would like to hide the UISearchBar most of the time and only call it to appear when user wants it.

I've put a UISearchBar in Interface Builder and hide it behind a view, when user click a button, it calls the following code, which I hoped it would bring the search bar to the front and slide the keyboard to view. But it doesn't….

- (IBAction)search:(id)sender
{
   [mySearchBar performSelector:@selector(searchBarTextDidBeginEditing:)];
}

Anyone have any idea how to activate UISearchBar by code?

Thanks.

Thanks for your replies. I just post the complete code in case someone's interested

- (IBAction)search:(id)sender
{
    mySearchBar.hidden = NO;
    [mySearchBar becomeFirstResponder];
}

#pragma mark UISearchBarDelegate delegate methods

// called when keyboard search button pressed
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    mySearchBar.hidden = YES;
    [mySearchBar resignFirstResponder];
// Do something with the mySearchBar.text
}

// called when cancel button pressed
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    mySearchBar.hidden = YES; 
    [mySearchBar resignFirstResponder];
}

Best Solution

If you want to pop the keyboard, you'll need to call [mySearchBar becomeFirstResponder]