为了理解UISearchDisplayController如何工作,我在viewDidLoad方法中编写了以下代码:
UISearchBar * searchBar = [[[UISearchBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 40.0f)] autorelease];
searchBar.delegate = self;
searchBar.showsCancelButton = YES;
_searchController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
_searchController.delegate = self;
_searchController.searchResultsDataSource = self;
_searchController.searchResultsDelegate = self;
[_searchController setActive:YES animated:YES];
[_searchController.searchBar becomeFirstResponder];
但是当我运行我的代码时,UISearchBar没有显示,因为你可以在图像中看到。
答案 0 :(得分:1)
您可以创建UISearchBarcontroller,但不会在uiviewcontroller中添加。所以你在uiviewcontroller中添加了UISearchBarcontroller。
编码:
[self.view addsubview:_searchController];