views:

217

answers:

1

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?

+1  A: 

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

vodkhang
Thank you, I needed a second set of eyes to see that. Should have checked the UISearchBarDelegate protocol one more time! That fixed it.
phooze
That's normal. XCode poorly supports auto complete and checking. I also did some same mistake with delegate and inheritance. I often do viewWillAppear{} instead of viewWillAppear:(BOOL)animated
vodkhang