0

为了了解 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 没有显示,如您在图像中看到的那样。

4

1 回答 1

1

您创建的 UISearchBarcontroller 很好但没有添加到 uiviewcontroller 中。所以你在 uiviewcontroller 中添加 UISearchBarcontroller。

编码:

[self.view addsubview:_searchController];

于 2012-08-02T13:33:27.260 回答