Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 11-06-2011, 10:55 AM   #1 (permalink)
Registered Member
 
Join Date: Nov 2011
Posts: 6
yassassin is on a distinguished road
Default XCode 4.2, Storyboard and Search Bar

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
yassassin is offline   Reply With Quote
Old 11-07-2011, 10:29 AM   #2 (permalink)
Registered Member
 
Join Date: Nov 2011
Posts: 6
yassassin is on a distinguished road
Default

Please, someone can help me on this topic?
I'm still stuck on!

Regards,
yassa
yassassin is offline   Reply With Quote
Old 11-07-2011, 11:08 AM   #3 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 83
vogueestylee is on a distinguished road
Default

OT: keep in mind, when using storyboard, you cannot compile for 3.1.3.. minimum is 4.2 as target fw, what is not always good...
vogueestylee is offline   Reply With Quote
Reply

Bookmarks

Tags
search bar, storyboard, uisearchbar, xcode

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 403
18 members and 385 guests
7twenty7, Alex-alex, Apptronics RBC, baja_yu, dre, FrankWeller, gwelmarten, ipodphone, jeroenkeij, jleannex55, matador1978, mbadegree, n00b, pbart, reficul, Retouchable, Sami Gh, usernametaken
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,676
Threads: 94,125
Posts: 402,910
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jleannex55
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 06:20 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0