你好。
我在使用自定义的UISearchController子类VLSearchController时出现了问题,它带有一个自定义的UISearch Bar,在我的UIViewController中无法调用任何委托方法。我的自定义子类没有自己的委托方法,但我认为由于它们是子类... 它们的委托方法也将被继承,并且需要在初始化UISearchController的UIViewController中定义。
在初始化UISearchController后,我将其委托和搜索栏的委托都设置为UIViewController:
_searchController = [[VLSearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchBar.delegate = self;
self.searchController.delegate = self;
在我的UIViewController中,以下委托方法没有被调用:
#pragma mark - UISearchBarDelegate
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar;
#pragma mark - UISearchController
- (void)willPresentSearchController:(UISearchController *)searchController;
#pragma mark - UISearchResultsUpdating
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController;
让我也提一下,我对UISearchController和UISearchBar进行子类化的原因是为了去掉取消按钮。 我需要给子类添加代理方法吗?如果需要,是否可以有人解释一下这些代理方法会做什么?
编辑:添加代码
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
和UISearchBarDelegate
协议? - Praveen Gowda I V