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.
(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
searchBarSearchButtonClickedis 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With