簡體   English   中英

為什么我的UISearchController委托方法沒有被調用?

[英]Why are my UISearchController delegate methods not being called?

我在課堂上加入了以下內容:

@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方法中創建了一個同名的局部變量。 因此,最后您永遠都不會設置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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM