3
votes

Aloha.

I am having trouble getting a custom subclass of UISearchController, VLSearchController, with a custom UISearch Bar to call any of its delegate methods in my UIViewController. My custom subclasses don't have any delegate methods of their own, but I thought that since they were subclasses... their delegate methods would be subclassed as well and need to be defined inside of the UIViewController that initializes the UISearchController.

After initializing the UISearchController, I set its delegate and the searchbar's delegate to the UIViewController:

_searchController = [[VLSearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchBar.delegate = self; 
self.searchController.delegate = self;

The following delegate methods in my UIViewController don't get called:

#pragma mark - UISearchBarDelegate
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar;

#pragma mark - UISearchController
- (void)willPresentSearchController:(UISearchController *)searchController;

#pragma mark - UISearchResultsUpdating
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController;

Let me also mention that the reason that I subclassed the UISearchController as well as the UISearchBar was to get rid of the cancel button. Do I need to add delegate methods to the subclasses? If so, would anybody mind explaining what these delegate methods would do?

EDIT: Addition of Code

UIViewController:

-(void) viewDidLoad
{

    [super viewDidLoad];

    //// Search & Results Stuff
    _searchController = [[VLSearchController alloc] initWithSearchResultsController:nil];
    self.searchController.searchResultsUpdater = self;

    [self.searchController.searchBar sizeToFit];
    self.searchController.searchBar.barTintColor = UIColorFromRGB(0x411229);

    [self.sfView addSubview:self.searchController.searchBar];

    self.searchController.delegate  = self;
    self.searchController.dimsBackgroundDuringPresentation = NO;
    self.searchController.searchBar.delegate = self;
    self.definesPresentationContext = YES;
} 

- (void)viewDidAppear:(BOOL)animated 
{

    [super viewDidAppear:animated];

    if (self.searchControllerWasActive) {
        self.searchController.active = self.searchControllerWasActive;
        _searchControllerWasActive = NO;

        if (self.searchControllerSearchFieldWasFirstResponder) {
            [self.searchController.searchBar becomeFirstResponder];
            _searchControllerSearchFieldWasFirstResponder = NO;
        }
    }
}

#pragma mark - UISearchBarDelegate

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar 
{
    [searchBar resignFirstResponder];
}

#pragma mark - UISearchController
- (void)willPresentSearchController:(UISearchController *)searchController 
{
    self.searchController.hidesNavigationBarDuringPresentation = false; // stop from animating
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    self.searchController.searchBar.showsCancelButton = false;
}

- (void)didDismissSearchController:(UISearchController *)searchController{
    filteredPics=_pics;
}

#pragma mark - UISearchResultsUpdating

- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {};

VLSearchController

@interface VLSearchController () 
@end

@implementation VLSearchController{
    UISearchBar *_searchBar;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

-(UISearchBar *)searchBar 
{

    if (_searchBar == nil) {
        _searchBar = [[VLSearchBar alloc] initWithFrame:CGRectZero];
    }
    return _searchBar;
}

   -(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText 
{
        if ([searchBar.text length] > 0) {
            self.active = true;
        } else {
            self.active = false;
        }
    }
@end

VLSearchBar

@implementation VLSearchBar

-(void)setShowsCancelButton:(BOOL)showsCancelButton {
    // Do nothing...
}

-(void)setShowsCancelButton:(BOOL)showsCancelButton animated:(BOOL)animated {
    // Do nothing....
}

@end
3
Does you class conform to UISearchResultsUpdating and UISearchBarDelegate protocol?Praveen Gowda I V
Yes, I have added the protocols <UISearchBarDelegate, UISearchControllerDelegate, UISearchResultsUpdating> to my UIViewController class.user2253546
Can you post the complete code (related to UISearchController)?Praveen Gowda I V
I have posted the UIViewController that uses the custom UISearchController (VLSearchController) as well as the VLSearchController and VLSearchBar code.user2253546

3 Answers

1
votes

It looks like you are setting the search bar's delegate to be your instance of VLSearchController. However that class does not conform to the UISearchBarDelegate protocol. I believe you want to set _searchBar.delegate = _searchBar where you have implemented your delegate methods.

There can only be one delegate for the searchBar so decide which class you want to handle text changes and showing/hiding the cancel button and set the delegate accordingly. You also set the searchBar delegate to be the UIViewController in your viewDidLoad method.

In general, check to make sure you are setting all your delegates properly to ensure that their methods get called.

I believe you will need your instance variable to be an instance of your search bar subclass as well.

0
votes

I found the solution to my own problem. The solution is to move the following delegate method:

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [searchBar resignFirstResponder];
}

into VLSearchController and setting _searchBar.delegate=self inside of VLSearchController's -(UISearchBar *)searchBar.

-1
votes

Call the delegates to connect a delegate to the search bar. Like so

 @property(nonatomic, assign) id< UISearchControllerDelegate >     _searchController;


  _searchController = [[VLSearchController alloc]             
initWithSearchResultsController:nil];
self.searchController.searchBar.delegate = self; 
self.searchController.delegate = self;


#pragma mark - UISearchBarDelegate
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {}

 #pragma mark - UISearchController
 - (void)willPresentSearchController:(UISearchController *)searchController;

 #pragma mark - UISearchResultsUpdating
 - (void)updateSearchResultsForSearchController:(UISearchController   
 *)searchController;

Hope that this helps you if not sorry but this should work Have A Nice Day! =)