I want to have a simple SearchBar in ObjectiveC. Using UISearchBar
or UISearchBarDelegate
is confusing me. I could have used a UITextField
but it does not have the look & feel of a search bar.
As in the image attached, I want just the searchbar no UITableView
associated with it. The image has a TableView attached but you get the point. Also after someone enters text into the searchBar & pressed "enter" how do I retrieve the text?
UPDATE: I am aware of these links which discuss the same, but they are more in light with using tables.
http://blog.webscale.co.in/?p=228
http://ved-dimensions.blogspot.com/2009/02/iphone-development-adding-search-bar-in.html
How to implement search bar in iPhone?
UISearchBar Sample Code
UISearchBar in UITableViewController?
Just make your view controller implement the UISearchBarDelegate
. In your xib file, all you need to do is to add a UISearchBar to your view and configure it as necessary, create an outlet for it (optional really but helps to be explicit), and assign the delegate outlet to your view controller.
Then, to respond to the search bar events, implement the UISearchBarDelegate
protocol methods as necessary. For example:
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
[self handleSearch:searchBar];
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
[self handleSearch:searchBar];
}
- (void)handleSearch:(UISearchBar *)searchBar {
NSLog(@"User searched for %@", searchBar.text);
[searchBar resignFirstResponder]; // if you want the keyboard to go away
}
- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar {
NSLog(@"User canceled search");
[searchBar resignFirstResponder]; // if you want the keyboard to go away
}