UISearchBar not responding to delegate methods

I have a UISearchBar placed inside of a view that's loaded through a tab bar controller, with the UISearchBar's delegate binded to the view's controller through interface builder, however my controller does not intercept any delegate methods, it's like they don't exist.

Is there any way to get this working through interface builder or will I have to resort to hard coding the entire layout?

Macbook Pro, Mac OS X (10.5.4)

Posted on Jul 19, 2008 9:45 AM

13 replies

Jul 19, 2008 9:55 AM in response to Community User

1. Double-check your connections. I know you think they're right, but it's very easy to mess up and would cause exactly the kind of problem you're seeing.

2. Double-check the spelling on your delegate methods.

3. If neither of those turn anything up, add this to your view controller:


- (BOOL)respondsToSelector:(SEL)sel {
NSLog(@"Queried about %@", NSStringFromSelector(sel));
return [super respondsToSelector:sel];
}


That'll tell you every time another class tries to check which methods your class supports. UISearchBar uses this to check if your controller implements the delegate methods. If you don't see any searchBar:* method checks in your Console pane, the connection between the search bar and your controller is being broken somehow; if you do, it's looking for the methods but not seeing them for some reason.

Jul 19, 2008 10:53 AM in response to Community User

Then there's something wrong with the connection. To confirm that, we'll make the connection manually. Create an outlet in your view controller's header file:

@interface MyViewController : UIViewController <UISearchBarDelegate> {
...
IBOutlet UISearchBar * searchBar;
...
}
...


In Interface Builder, connect your search bar to the new searchBar outlet.

Back in Xcode, go to the implementation of your viewDidLoad method and add this line:

searchBar.delegate = self;


This will create the connection manually.

Save, build, and test. Did that help?

Jul 19, 2008 10:59 AM in response to Brent Royal-Gordon

I get the following error when navigating to the view holding the uisearchbar.

2008-07-19 18:58:50.035 Helpdesk[3408:20b] * Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UIViewController 0x407b90> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key searchBar.'

Jul 20, 2008 8:25 AM in response to Community User

Strange. That "undefined key" error basically means that it couldn't find your searchBar property when it had an actual object in front of it.

Okay, next dumb question. Are you sure you're actually allocating an object of your view controller class, or are you allocating a generic UIViewController? (This isn't about what class File's Owner is set to--the view controller object is created somewhere else, either in another class or in another nib.)

Jul 20, 2008 8:27 AM in response to Brent Royal-Gordon

I'm not doing anything like that, I've literally left everything as what xcode gives to me when I create a new tab bar based project. All my views and everything are in interface builder as generated for me by xcode.

Try doing this yourself and see if you can get it working, because it isn't for me no matter what I do.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

UISearchBar not responding to delegate methods

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.