You get a set of requirements, you build an app. You are happy. You get another requirement to add a search bar to your address book. Easy peazy you say. You drag it from Interface Builder toolset to your Root View Controller with the table view. Its done already. You had just yawned, you were tired after all and ??????? The Search Bar isn't there. Don't panic. All you have to do is code. Here is how we add the search bar programatically.
// //Edit your interface class to add the UISearchBarDelegate, a UISearchBar element and a UISearchDisplayController
@interface RootViewController : UITableViewController<UISearchBarDelegate>
{
UISearchBar *searchBar;
UISearchDisplayController *searchDC;
}
@property(nonatomic, retain) UISearchBar *searchBar;
@property(nonatomic, retain) UISearchDisplayController *searchDC;
@end
// // Now moving on to your implementation class
//Synthesize the elements
@implementation RootViewController
@sysnthesize searchBar, searchDC;
//Now we will add the search bar using the -(void)viewDidLoad method
-(void) viewDidLoad{
self.searchBar=[[[UISearchBar alloc] initWithFrame:CGRectMake(0.0f , 0.0f, 320.0f,44.0f)] autorelease];
self.searchBar.autocorrection;
self.searchBar.autocapitalization;
self.searchBar.keyboard;
self.searchBar.bar;
self.searchBar.delegate=self;
//Adding the search bar to the table
self.tableView.tableHeaderView=self.searchBar;
//Setting the delegate and the data source for the search display controller.
self.searchDC = [[[UISearchDisplayController alloc]initWithSearchBar:self.searchBar contentsController:self] autorelease];
self.searchDC.searchResultsDataSource = self;
self.searchDC.searchResultsDelegate = self;
}
//Dont forget to release the search bar and its display controller
-(void) dealloc{
[searchBar release];
[searchDC release];
[super dealloc];
}
That's it. You are done adding the search bar to the code,
P.S. : To implement the search logic overide the - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{ } method.
// //Edit your interface class to add the UISearchBarDelegate, a UISearchBar element and a UISearchDisplayController
@interface RootViewController : UITableViewController<UISearchBarDelegate>
{
UISearchBar *searchBar;
UISearchDisplayController *searchDC;
}
@property(nonatomic, retain) UISearchBar *searchBar;
@property(nonatomic, retain) UISearchDisplayController *searchDC;
@end
// // Now moving on to your implementation class
//Synthesize the elements
@implementation RootViewController
@sysnthesize searchBar, searchDC;
//Now we will add the search bar using the -(void)viewDidLoad method
-(void) viewDidLoad{
self.searchBar=[[[UISearchBar alloc] initWithFrame:CGRectMake(0.0f , 0.0f, 320.0f,44.0f)] autorelease];
self.searchBar.autocorrection;
self.searchBar.autocapitalization;
self.searchBar.keyboard;
self.searchBar.bar;
self.searchBar.delegate=self;
//Adding the search bar to the table
self.tableView.tableHeaderView=self.searchBar;
//Setting the delegate and the data source for the search display controller.
self.searchDC = [[[UISearchDisplayController alloc]initWithSearchBar:self.searchBar contentsController:self] autorelease];
self.searchDC.searchResultsDataSource = self;
self.searchDC.searchResultsDelegate = self;
}
//Dont forget to release the search bar and its display controller
-(void) dealloc{
[searchBar release];
[searchDC release];
[super dealloc];
}
That's it. You are done adding the search bar to the code,
P.S. : To implement the search logic overide the - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{ } method.