Hello,
I'm going crazy with managing the Search Bar within the Storyboard

.
I've created my TableViewController and I've added the UISearchBar to the user interface. Everything seems ok, except for the "Search" button: I've created a method to hide the Search Bar, but I'm not able to intercept the button pressure.
Below my code:
Code:
// HelloSearchTableViewController_001.h
#import <UIKit/UIKit.h>
@interface HelloSearchTableViewController_001 : UITableViewController
{
NSMutableArray *listOfNames;
NSMutableArray *copyListOfNames;
IBOutlet UISearchBar *searchBar;
BOOL searching;
BOOL letUserSelectRow;
}
- (void) searchTableView;
- (void) doneSearching_Clicked:(id)sender;
//- (IBAction)doneSearching_Clicked:(id)sender;
- (IBAction)backgroundTouched:(id)sender;
@end
(I've tried to declare "doneSearching_Clicked" method in both "void" and "IBAction" manners, with no results)
Code:
// HelloSearchTableViewController_001.m
#import "HelloSearchTableViewController_001.h"
@implementation HelloSearchTableViewController_001
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
//---initialize the arrays---
listOfNames = [[NSMutableArray alloc] init];
copyListOfNames = [[NSMutableArray alloc] init];
//---add items---[listOfNames addObject:@"Alessandro"];[listOfNames addObject:@"Bruno"];[listOfNames addObject:@"Carlo"];[listOfNames addObject:@"Francesco"];[listOfNames addObject:@"Roberto"];[listOfNames addObject:@"Mauro"];[listOfNames addObject:@"Luca"];
[super viewDidLoad];
//Add the search bar
// self.tableView.tableHeaderView = searchBar;
searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
searching = NO;
letUserSelectRow = YES;
}
// raised when the user touches the search bar to bring up the keyboard
- (void) searchBarTextDidBeginEditing:(UISearchBar *)theSearchBar
{
searching = YES;
letUserSelectRow = NO;
self.tableView.scrollEnabled = NO;
}
// raised when a specified row is about to be selected
- (NSIndexPath *)tableView :(UITableView *)theTableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//prevents the user from selecting a row if the variable “letUserSelectRow” isn't set to true
if(letUserSelectRow)
return indexPath;
else
return nil;
}
//raised when the user starts typing in the search field
- (void)searchBar:(UISearchBar *)theSearchBar textDidChange:(NSString *)searchText
{
[copyListOfNames removeAllObjects];
if([searchText length] > 0)
{
searching = YES;
letUserSelectRow = YES;
self.tableView.scrollEnabled = YES;
[self searchTableView];
}
else
{
searching = NO;
letUserSelectRow = NO;
self.tableView.scrollEnabled = NO;
}
[self.tableView reloadData];
}
//raised when the user taps on the search keyboard
- (void) searchBarSearchButtonClicked:(UISearchBar *)theSearchBar
{
[self searchTableView];
}
- (void) searchTableView
{
NSString *searchText = searchBar.text;
NSMutableArray *searchArray = [[NSMutableArray alloc] init];
[searchArray addObjectsFromArray:listOfNames];
for (NSString *sTemp in searchArray)
{
NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (titleResultsRange.length > 0)
{
[copyListOfNames addObject:sTemp];
}
}
searchArray = nil;
}
//custom raised when the user clicks the done button****
- (void) doneSearching_Clicked:(id)sender
//- (IBAction)doneSearching_Clicked: (id)sender
{
searchBar.text = @"";
//dismiss the keyboard
[searchBar resignFirstResponder];
//[sender resignFirstResponder];
letUserSelectRow = YES;
searching = NO;
self.navigationItem.rightBarButtonItem = nil;
self.tableView.scrollEnabled = YES;
[self.tableView reloadData];
}
//custom raised when the user touch background****
-(IBAction)backgroundTouched:(id)sender
{
[searchBar resignFirstResponder];
}
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField
{
NSLog(@"%@ textFieldShouldReturn", [self class]);
[theTextField resignFirstResponder];
// do stuff with the text
NSLog(@"text = %@", [theTextField text]);
return YES;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
if (searching)
return 1;
else
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
if (searching)
return [copyListOfNames count];
else
return[listOfNames count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
NSString *cellValue;
if(searching)
cellValue = [copyListOfNames objectAtIndex:indexPath.row];
else
cellValue =[listOfNames objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue; //name value
return cell;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
*/
}
@end
The problem is with "doneSearching_Clicked" method, since I don't know how to tell to Xcode that when I press the Search button, this method must be invoked.
Thank you in advance for your support,
yassa