简体   繁体   中英

How can I get the searchBarDelegate methods with a custom UISearchBar?

I made a custom search bar for my app, here is the class: .h

@interface SearchView : UIView <UITextFieldDelegate, UISearchBarDelegate>
@property (nonatomic, strong) UITextField *searchField;
@property (nonatomic, strong) UIButton *searchButton;
@end

.m

@implementation SearchView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

      CGRect frame = CGRectMake(0, 0, 35, 35);

      [self.searchButton sizeToFit];
      self.alpha = 0.6;
      self.backgroundColor = [UIColor colorWithRed:206/255.0 green:206/255.0 blue:206/255.0 alpha:.8];
      self.searchButton = [UIButton buttonWithType:UIButtonTypeCustom];
      //[self.searchButton addTarget:self action:@selector(performSearchQuery) forControlEvents:UIControlEventTouchUpInside];
      [self.searchButton setImage:[UIImage imageNamed:@"SearchIcon2"] forState:UIControlStateNormal];
      //[self.searchButton sizeToFit];
      //self.searchButton.bounds.size.height = ;
      self.searchButton.frame = frame;
     // self.searchButton.right = frame.size.width - kPadding;
      [self addSubview:self.searchButton];

      self.searchField = [[UITextField alloc] initWithFrame:CGRectMake(43, -5, 270, 50)];
      //self.searchField.placeholder = [@"Search..." uppercaseString];
      self.searchField.delegate = self;
      self.searchField.autocorrectionType = UITextAutocorrectionTypeNo;
      self.searchField.autocapitalizationType = UITextAutocapitalizationTypeWords;
      self.searchField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
      self.searchField.returnKeyType = UIReturnKeySearch;
      self.searchField.clearsOnBeginEditing = YES;
      self.searchField.clearButtonMode = UITextFieldViewModeWhileEditing;
      [self addSubview:self.searchField];

    }
    return self;
}

I'm trying to get the functionality of the searchBarSearchButtonClicked: delegate method.

Here is the code for that (taskListViewController):

.h

@interface TaskListViewController : UIViewController <UITableViewDelegate,UITableViewDataSource, UISearchBarDelegate, UITextFieldDelegate, ScavengerAPIGetTaskListDelegate>{
    SearchView *searchView;
}

.m

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
  searchBar = (UISearchBar *)searchView;

  [self didTapSearchButton];
}

- (IBAction)didTapSearchButton

    {
      NSString *searchTerm = searchView.searchField.text;

      if ([searchTerm isEqualToString:@""]) {
        [self.view endEditing:YES];
        return ;
      }

      [searchView resignFirstResponder];
      NSArray *results = [[CoreDataManager sharedInstance] fetchTaskByName:searchTerm];

      [tasks removeAllObjects];
      [tasks addObjectsFromArray:results];

      [self.view endEditing:YES];
      [taskTable reloadData];
    }

So i set up the uisearchBarDelegate on both the searchView class and TaskListViewController. Is the reason it is not getting called because I am subclassing uiview instead of uisearchBar? If you need more code/details, let me know! Thanks!

You have created a custom search view so it is advised to make your class implement its own search methods of delegate instead of using existing UISearchBar one to differentiate and make the code more readable..

You should make a delegate for your SearchView class

Here you can refer how to create one of your own How to make Custom Delegate in iOS app

After creation TaskListViewController can conform to that delegate and you can call the method inside searchview when searchButton is tapped so that TaskListViewController recieves it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM