为什么我的 UISearchController 委托方法没有被调用?
Why are my UISearchController delegate methods not being called?
我在 class 中加入了以下内容:
@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
确实出现在了它应该出现的位置,并且在点击搜索栏时显示了键盘,但是我的 none正在调用委托方法。怎么回事?
您有一个名为 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;
我在 class 中加入了以下内容:
@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
确实出现在了它应该出现的位置,并且在点击搜索栏时显示了键盘,但是我的 none正在调用委托方法。怎么回事?
您有一个名为 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;