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
UISearchResultsUpdating
andUISearchBarDelegate
protocol? – Praveen Gowda I V