ios - UISearchController & UISearchBar 子类

标签 ios objective-c delegates uisearchbar uisearchcontroller

你好。

我在获取 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/

相关文章:

ios - 具有自定义图形的 xcode 选项卡 Controller ,在第一次单击之前不会加载

ios - 克隆替换我的 iPhone 设备和模拟器上以前的应用程序

c++ - 使用全局变量的 Xcode C++ 单元测试

swift - 如何使用委托(delegate)模式将对象传输到另一个 VC?

ios - 已弃用 : FCM direct channel is deprecated, 请使用 APNs 进行下游消息处理

ios - 在 Mac 上哪里可以找到内存监视器工具?

ios - 具有核心数据和 API 请求的多线程

c# - 动态生成调用 Lambda 函数的按钮——变量作用域

c# - Linq lambda 不工作但委托(delegate)不工作

iphone - 无法将 OCMock header 导入 iOS 项目