【发布时间】: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