So I’ve defined an IBAction in my header, implemented it in my main and I can see it in IB – however, when I drag over the UISearchBar, it doesn’t connect.
Here’s my header code:
#import <UIKit/UIKit.h>
@class DetailViewController;
@interface CustomerListViewController : UITableViewController
{
}
-(IBAction)filterTableData:(UISearchBar *)filterBar;
@property (strong, nonatomic) DetailViewController *detailViewController;
@property (retain) IBOutlet UISearchBar *searchBar;
@property (strong, nonatomic)NSArray *custs, *workingSet;
@end
Here’s my implementing method:
-(IBAction)filterTableData:(UISearchBar *)filterBar {
//filter our search results
NSString *filter;
if (filterBar.text)
filter = [filterBar.text stringByAppendingString:@"*"];
else
filter = @"*";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF LIKE[cd] %@", filter];
self.workingSet = [self.custs filteredArrayUsingPredicate: predicate];
[self.tableView reloadData];
}
And here’s an image of IB actually seeing the method and allowing it to be dragged, and me unsuccessfully attempting to drag it to the search bar:
A UISearchBar is not a subclass of a UIControl, meaning that you can’t add methods to be called when an event occurs.
What you’ll want to do is setup your view controller as a UISearchBarDelegate and use the following method instead of your
filterTableData:method.- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchTextThen in IB you’ll want to click and drag from the search bar to the files owner and choose the delegate property.