1

The searchBarBookmarkButtonClicked method isn't firing when I tap on the search bar. I have already added the self.searchBar.deleagate = self; and in my .h file I added the <UISearchBarDelegate>.

.h file:

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>

@interface ViewController : UIViewController <UISearchBarDelegate,MKMapViewDelegate>

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

@property (strong, nonatomic) IBOutlet MKMapView *myMap;

@end

.m file:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

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

- (void)searchBarBookmarkButtonClicked:(UISearchBar * )searchBar{
NSLog(@"working");

[self.searchBar resignFirstResponder];
NSLog(@"working");

CLGeocoder *geocoder = [[CLGeocoder alloc] init];

[geocoder geocodeAddressString:self.searchBar.text completionHandler:^(NSArray *placemarks, NSError *error) {
    CLPlacemark *placemark = [placemarks objectAtIndex:0];
    MKCoordinateRegion region;
    CLLocationCoordinate2D newLocation = [placemark.location coordinate];
    region.center = [(CLCircularRegion *)placemark.region center];

    MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
    [annotation setCoordinate:newLocation];
    [annotation setTitle:self.searchBar.text];
    [self.myMap addAnnotation:annotation];

    MKMapRect mr = [self.myMap visibleMapRect];
    MKMapPoint pt = MKMapPointForCoordinate([annotation coordinate]);
    mr.origin.x = pt.x - mr.size.width * 0.5;
    mr.origin.y = pt.y -mr.size.width * 0.25;
    [self.myMap setVisibleMapRect:mr animated:YES];
}];

}

@end
Jay Dave
  • 297
  • 4
  • 11

3 Answers3

2

Make sure you have

  1. All delegate being set properly, this can be done either in code or in storyboard.
  2. Make use of correct delegate method to capture the event.

If you think

- (void)searchBarBookmarkButtonClicked:(UISearchBar * )searchBar

then try using the other method which is more straight forward to trigger search

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
Rahul Sharma
  • 3,013
  • 1
  • 20
  • 47
1

Try this UISearchBar delegate method:

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
Kusal Shrestha
  • 1,643
  • 2
  • 13
  • 20
  • Thank you! that worked for being able to fire it, but I do not know if that is the proper method to use. Only because now when I run my entire code none of my annotations are working and my pin is thrown somewhere in the Africa – Jay Dave Aug 20 '15 at 17:19
0

Thank you @Rahul! Turns out I was using the wrong method!

.h:

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>

@interface ViewController : UIViewController <UISearchBarDelegate,MKMapViewDelegate>

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

  @property (strong, nonatomic) IBOutlet MKMapView *myMap;

@end

.m:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

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

- (void)searchBarBookmarkButtonClicked:(UISearchBar * )searchBar{

[self.searchBar resignFirstResponder];

CLGeocoder *geocoder = [[CLGeocoder alloc] init];

[geocoder geocodeAddressString:self.searchBar.text completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *placemark = [placemarks objectAtIndex:0];
MKCoordinateRegion region;
CLLocationCoordinate2D newLocation = [placemark.location coordinate];
region.center = [(CLCircularRegion *)placemark.region center];

MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
[annotation setCoordinate:newLocation];
[annotation setTitle:self.searchBar.text];
[self.myMap addAnnotation:annotation];

MKMapRect mr = [self.myMap visibleMapRect];
MKMapPoint pt = MKMapPointForCoordinate([annotation coordinate]);
mr.origin.x = pt.x - mr.size.width * 0.5;
mr.origin.y = pt.y -mr.size.width * 0.25;
[self.myMap setVisibleMapRect:mr animated:YES];
}];

}

@end
Jay Dave
  • 297
  • 4
  • 11