你好。
我在获取 UISearchController 的自定义子类 VLSearchController 时遇到问题,该子类使用自定义 UISearch Bar 在我的 UIViewController 中调用它的任何委托(delegate)方法。我的自定义子类没有自己的任何委托(delegate)方法,但我认为既然它们是子类......它们的委托(delegate)方法也将被子类化并且需要在初始化 UISearchController 的 UIViewController 内部定义。
初始化 UISearchController 后,我将其委托(delegate)和搜索栏的委托(delegate)设置为 UIViewController:
_searchController = [[VLSearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchBar.delegate = self;
self.searchController.delegate = self;
我的 UIViewController 中的以下委托(delegate)方法未被调用:
#pragma mark - UISearchBarDelegate
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar;
#pragma mark - UISearchController
- (void)willPresentSearchController:(UISearchController *)searchController;
#pragma mark - UISearchResultsUpdating
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController;
我还要提一下,我将 UISearchController 和 UISearchBar 子类化的原因是为了摆脱取消按钮。 我需要向子类添加委托(delegate)方法吗?如果是这样,有人介意解释一下这些委托(delegate)方法的作用吗?
编辑:添加代码
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
最佳答案
看起来您正在将搜索栏的委托(delegate)设置为您的 VLSearchController 实例。但是该类不符合 UISearchBarDelegate 协议(protocol)。我相信您想在实现委托(delegate)方法的地方设置 _searchBar.delegate = _searchBar
。
searchBar 只能有一个委托(delegate),因此请确定要处理文本更改和显示/隐藏取消按钮的类,并相应地设置委托(delegate)。您还在 viewDidLoad 方法中将 searchBar 委托(delegate)设置为 UIViewController。
一般来说,检查以确保正确设置所有委托(delegate),以确保调用它们的方法。
我相信您也需要将实例变量作为搜索栏子类的实例。
关于ios - UISearchController & UISearchBar 子类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30020853/