UISearchController & UISearchBar 子类
UISearchController & UISearchBar Subclass
你好。
我在获取 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
调用代理将代理连接到搜索栏。像这样
@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;
如果不是抱歉,希望这对您有所帮助,但这应该有效祝您有美好的一天! =)
您似乎正在将搜索栏的委托设置为您的 VLSearchController 实例。但是 class 不符合 UISearchBarDelegate 协议。我相信您想在实现委托方法的地方设置 _searchBar.delegate = _searchBar
。
searchBar 只能有一个委托,因此请决定要处理哪个 class 文本更改和 showing/hiding 取消按钮并相应地设置委托。您还在 viewDidLoad 方法中将 searchBar 委托设置为 UIViewController。
一般来说,检查以确保正确设置所有委托,以确保调用它们的方法。
我相信您也需要将实例变量作为搜索栏子class的实例。
我找到了解决我自己问题的方法。解决方案是移动以下委托方法:
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[searchBar resignFirstResponder];
}
进入 VLSearchController 并在 VLSearchController 的 -(UISearchBar *)searchBar
内部设置 _searchBar.delegate=self
。
你好。
我在获取 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
调用代理将代理连接到搜索栏。像这样
@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;
如果不是抱歉,希望这对您有所帮助,但这应该有效祝您有美好的一天! =)
您似乎正在将搜索栏的委托设置为您的 VLSearchController 实例。但是 class 不符合 UISearchBarDelegate 协议。我相信您想在实现委托方法的地方设置 _searchBar.delegate = _searchBar
。
searchBar 只能有一个委托,因此请决定要处理哪个 class 文本更改和 showing/hiding 取消按钮并相应地设置委托。您还在 viewDidLoad 方法中将 searchBar 委托设置为 UIViewController。
一般来说,检查以确保正确设置所有委托,以确保调用它们的方法。
我相信您也需要将实例变量作为搜索栏子class的实例。
我找到了解决我自己问题的方法。解决方案是移动以下委托方法:
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[searchBar resignFirstResponder];
}
进入 VLSearchController 并在 VLSearchController 的 -(UISearchBar *)searchBar
内部设置 _searchBar.delegate=self
。