OxenBoxen
OxenBoxen

Reputation: 165

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!

Upvotes: 0

Views: 768

Answers (1)

Shubhank
Shubhank

Reputation: 21805

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

Upvotes: 1

Related Questions