UISearchController& UISearchBar子类

时间:2015-05-04 00:08:18

标签: ios objective-c delegates uisearchbar uisearchcontroller

阿罗哈。

我无法获得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

3 个答案:

答案 0 :(得分:1)

您似乎将搜索栏的委托设置为VLSearchController的实例。但是该类不符合UISearchBarDelegate协议。我相信你想设置_searchBar.delegate = _searchBar你已经实现了委托方法的地方。

searchBar只能有一个委托,因此决定要处理文本更改的类以及显示/隐藏取消按钮并相应地设置委托。您还将searchBar委托设置为viewDidLoad方法中的UIViewController。

一般情况下,请检查以确保正确设置所有代理,以确保调用其方法。

我相信您也需要将您的实例变量作为搜索栏子类的实例。

答案 1 :(得分:0)

我找到了解决自己问题的方法。解决方案是移动以下委托方法:

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [searchBar resignFirstResponder];
}

进入VLSearchController,并在VLSearchController的_searchBar.delegate=self内设置-(UISearchBar *)searchBar

答案 2 :(得分:-1)

呼叫代理人将代表连接到搜索栏。像这样

 @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;

希望这可以帮助你,如果不是抱歉,但这应该工作有一个美好的一天! =)