【问题标题】:Why are my UISearchController delegate methods not being called?为什么我的 UISearchController 委托方法没有被调用?
【发布时间】:2018-05-06 19:15:28
【问题描述】:

我在课堂上加入了以下内容:

@interface DiscoverNew() <UISearchResultsUpdating, UISearchBarDelegate, UISearchControllerDelegate>

我在这里设置了UISearchController

-(void)viewDidLoad {

       [self configureSearchController];

}  

-(void)configureSearchController{

        // Initialize and perform a minimum configuration to the search controller.
        UISearchController *searchController = self.searchController;
        searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
        searchController.searchResultsUpdater = self;
        searchController.dimsBackgroundDuringPresentation = NO;
        searchController.searchBar.placeholder = @"Search for friends...";
        searchController.delegate = self;
        searchController.searchBar.delegate = self;
        [searchController.searchBar sizeToFit];

        [searchController.searchBar setBarStyle:UIBarStyleBlack];
        [searchController.searchBar setSearchBarStyle:UISearchBarStyleMinimal];
        searchController.searchBar.tintColor = [GBColor accentGray];
        searchController.searchBar.backgroundColor = [GBColor darkGray];
        searchController.searchBar.barTintColor = [GBColor accentGray];
        searchController.searchBar.searchTextPositionAdjustment = UIOffsetMake(18.0f, 0.0f);

        // Place searchbar above TableView
        self.tableView.tableHeaderView = searchController.searchBar;

}

如您所见,我调用了searchController.searchBar.delegate = self; searchController 确实出现在它应该出现的位置,并且在点击搜索栏时会显示键盘,但没有调用我的委托方法。怎么回事?

【问题讨论】:

  • 可能是因为您的searchController 是一个没有强引用的局部变量。在configureSearchController 结束时,您创建的UISearchController 将被释放。
  • @maddy,我该如何解决?不熟悉强/弱引用及其作用。
  • 使searchController 成为实例变量而不是局部变量。
  • @maddy 我的 .h 文件中确实有 @property (nonatomic, strong) UISearchController *searchController;
  • 你能实现搜索栏委托方法吗?

标签: ios objective-c uisearchbar uisearchcontroller uisearchbardelegate


【解决方案1】:

您有一个名为searchController 的属性。但随后您在 configureSearchController 方法中创建了一个同名的局部变量。所以最后你永远不会设置 searchController 属性。结果,您在本地创建的搜索控制器超出范围,没有更多引用,并被释放。

解决方法是稍微更新您的代码。

UISearchController *searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
searchController.searchResultsUpdater = self;
searchController.dimsBackgroundDuringPresentation = NO;
searchController.searchBar.placeholder = @"Search for friends...";
searchController.delegate = self;
searchController.searchBar.delegate = self;
[searchController.searchBar sizeToFit];

[searchController.searchBar setBarStyle:UIBarStyleBlack];
[searchController.searchBar setSearchBarStyle:UISearchBarStyleMinimal];
searchController.searchBar.tintColor = [GBColor accentGray];
searchController.searchBar.backgroundColor = [GBColor darkGray];
searchController.searchBar.barTintColor = [GBColor accentGray];
searchController.searchBar.searchTextPositionAdjustment = UIOffsetMake(18.0f, 0.0f);

self.searchController = searchController;

【讨论】:

  • 工作完美。你的解释帮助我理解了我哪里出错了。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-13
  • 1970-01-01
  • 2013-12-03
  • 2020-11-28
相关资源
最近更新 更多