Click here to Skip to main content
15,886,078 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm really new to iOS development. I wanted to add a search bar to my app. When touching on the search bar key board pops up. But when I press the search button it doesn't dismiss the keyboard. My controller .h file is as belows.


C#
@interface SearchViewController : 
UIViewController<UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate>{
    
    IBOutlet UISearchBar *searchBar;
    IBOutlet UITableView *tableView;
    NSMutableArray *listOfItems;
    NSMutableArray *copyListOfItems;
    NSArray *songs;
    BOOL searching;
    BOOL letUserSelectRow;
}
@property(strong,nonatomic)NSArray *songs;
@property(strong,nonatomic)UITableView *tableView;   
@property(strong,nonatomic)UISearchBar *searchBar;
@end



I have implemented the following methods in .m file. But they don't invoke.

C#
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
     NSLog(@"search clicked");
    [self.searchBar resignFirstResponder];
}

-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
    NSLog(@"Cancel Clicked");
}


Can anybody explain me the reason?
Posted

Hi,
I don't see any reference to a UIButton class or IBOutlet linked to the search button or an IBAction method that will capture the button click event.
I think you will find the following link useful:
http://klanguedoc.hubpages.com/hub/IOS-5-Tutorial-on-How-To-Dismiss-the-UI-Keyboard[^]

If you still have problems after going through the article in above link, just put a shout :)
 
Share this answer
 
in .h file add delegate and initialize declare
Objective-C
@interface RestaurantViewController : UIViewController <uisearchdisplaydelegate,>

    UISearchDisplayController      *resName_search;
    UISearchBar *mySearchBar;


in .m file
Objective-C
    mySearchBar = [[UISearchBar alloc]init];
    [mySearchBar setDelegate:self];
//    [mySearchBar setShowsCancelButton:YES animated:NO];
    resName_search = [[UISearchDisplayController alloc] initWithSearchBar:mySearchBar contentsController:self];
    resName_search.delegate = self;
    resName_search.searchResultsDataSource = self;
    resName_search.searchResultsDelegate = self;
    [self.view addSubview:resName_search.searchBar];
   [resName_search.searchBar setFrame:CGRectMake(0.0, barOriginY+79.0, 106.0, 40.0)];

-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
NSLog(@" enter text clicked");

    return YES;
}
-(BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar
{

 NSLog(@"cancel clicked");
   return YES;

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900