簡體   English   中英

即使在viewDidLoad中設置了委托,UISearchBar也無法使用

[英]UISearchBar not working even with delegate set in viewDidLoad

當我點擊搜索欄時,不會觸發searchBarBookmarkButtonClicked方法。 我已經添加了self.searchBar.deleagate = self; 在我的.h文件中,添加了<UISearchBarDelegate>

.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{
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

確保你有

  1. 所有代表均已正確設置,可以在代碼或情節提要中完成。
  2. 利用正確的委托方法捕獲事件。

如果你認為

- (void)searchBarBookmarkButtonClicked:(UISearchBar * )searchBar

然后嘗試使用其他更直接觸發搜索的方法

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar

試試下面的UISearchBar委托方法:

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar

謝謝@Rahul! 原來我使用了錯誤的方法!

。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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM