Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

searchBarSearchButtonClicked not firing

I have researched this question. Looks like this is a common question but nothing I have tried has worked. I am very new to iPhone app development and Objective C. All I want to do at this point is type in a string in a search bar field and return the value in NSLog() when the Search button on the keypad is tapped.

My header file looks like this:

@interface ListingMapViewController : UIViewController <UISearchBarDelegate> 

@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;

@end

I have this in my class file:

#import "ListingMapViewController.h"

@interface ListingMapViewController ()


@end

@implementation ListingMapViewController


- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
searchBar.delegate =self;

[self handleSearch:searchBar];
NSLog(@"User searched for %@", searchBar.text);

}

@end

When I click the Search button nothing outputs. What am I missing? Thanks.

like image 905
user2350993 Avatar asked May 05 '13 01:05

user2350993


1 Answers

(Answered in a question edit. Converted to a community wiki answer. See Question with no answers, but issue solved in the comments (or extended in chat) )

The OP wrote:

Solved this by setting the searchBar delegate when the view loads. I guess setting it in searchBarSearchButtonClicked is too late.

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self.searchBar setDelegate:self];
 //self.searchBar.delegate = self; <-- also works
}

Now when I click the Search button on the keypad the text in the search bar field outputs to NSLog(). Hurray! On to the next challenge.

like image 175
2 revs Avatar answered Oct 19 '22 04:10

2 revs