Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UISearchBar delegate not called when used as UINavigationBar titleVIew?

I have a UITableViewController that I have specified as a UISearchBarDelegate. Up until now, I had programmatically added the UISearchBar to the headerView of the table, and there were no problems.

I began to run out of screen real estate, so I decided to kill my normal UINavigationController title (which was text), and added the following code, moving my SearchBar from the table to the UINavigationBar:

// (Called in viewDidLoad)
// Programmatically make UISearchBar
UISearchBar *tmpSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0,0,320,45)];
tmpSearchBar.delegate = self;
tmpSearchBar.showsCancelButton = YES;
tmpSearchBar.autocorrectionType = UITextAutocorrectionTypeNo;
tmpSearchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;
[self set_searchBar:tmpSearchBar];
[tmpSearchBar release];
self.navigationItem.titleView = [self _searchBar];

This code works as expected - my UINavigationBar is now a UISearchBar. However, my delegate method:

/** Only show the cancel button when the keyboard is displayed */
- (void) searchBarDidBeginEditing:(UISearchBar*) lclSearchBar
{
  lclSearchBar.showsCancelButton = YES;
}

...is no longer being called. I've breakpointed, and I've confirmed that the UISearchBar's delegate is indeed self, the view controller. Oddly, this delegate method is still called just fine:

/** Run the search and resign the keyboard */
- (void) searchBarSearchButtonClicked:(UISearchBar *)lclSearchBar
{
  _deepSearchRan = NO;
  [self runSearchForString:[[self _searchBar] text] isSlowSearch:NO];
  [lclSearchBar resignFirstResponder];
}

Any ideas why UINavigationBar is swallowing my delegate calls?? What am I missing?

like image 463
makdad Avatar asked Jun 13 '10 03:06

makdad


1 Answers

I think you write the wrong method signature. It should be : – searchBarTextDidBeginEditing: Here is all the UISearchBarDelegate methods for text editing.

– searchBar:textDidChange:

– searchBar:shouldChangeTextInRange:replacementText:

– searchBarShouldBeginEditing:

– searchBarTextDidBeginEditing:

– searchBarShouldEndEditing:

– searchBarTextDidEndEditing:

UISearchBarDelegate

like image 130
vodkhang Avatar answered Sep 23 '22 01:09

vodkhang