阿罗哈。
我很难获得UISearchController的自定义子类VLSearchController,还有一个自定义UISearch条来调用它在UIViewController中的任何委托方法。我的自定义子类没有它们自己的任何委托方法,但我认为由于它们是子类.它们的委托方法也将被子类化,并且需要在初始化UIViewController的UISearchController中定义。
初始化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进行子类的原因是为了去掉cancel按钮。是否需要将委托方法添加到子类中?如果是的话,有人介意解释一下这些委托方法会做什么吗?
编辑:添加代码
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;
}
}
@endVLSearchBar
@implementation VLSearchBar
-(void)setShowsCancelButton:(BOOL)showsCancelButton {
// Do nothing...
}
-(void)setShowsCancelButton:(BOOL)showsCancelButton animated:(BOOL)animated {
// Do nothing....
}
@end发布于 2015-05-04 20:47:28
看起来,您将搜索栏的委托设置为VLSearchController的实例。但是,该类不符合UISearchBarDelegate协议。我相信您希望在实现委托方法的地方设置_searchBar.delegate = _searchBar。
searchBar只能有一个委托,因此决定要处理文本更改的类,显示/隐藏cancel按钮,并相应地设置委托。还将searchBar委托设置为viewDidLoad方法中的UIViewController。
通常,检查以确保正确设置所有委托,以确保调用它们的方法。
我相信您也需要您的实例变量作为搜索栏子类的实例。
发布于 2015-05-04 22:18:22
我找到了解决自己问题的办法。解决方案是移动以下委托方法:
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[searchBar resignFirstResponder];
}在VLSearchController的VLSearchController中设置_searchBar.delegate=self。
发布于 2015-05-04 01:12:57
调用委托将委托连接到搜索栏。就像这样
@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;希望这对你有帮助,如果你不感到抱歉,但这应该有一个美好的一天!
https://stackoverflow.com/questions/30020853
复制相似问题