【发布时间】:2015-07-07 16:14:28
【问题描述】:
我已经实现了一个 UISearchController,它的搜索栏位于导航栏中,我希望在加载视图时激活搜索栏。当我说活动时,我的意思是键盘出现并且用户可以在不点击搜索栏的情况下输入他/她的搜索。
我使用以下代码初始化了 UISearchController:
- (void)viewDidLoad
{
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
[self.searchController setSearchResultsUpdater:self];
[self.searchController setDimsBackgroundDuringPresentation:NO];
[self.searchController setHidesNavigationBarDuringPresentation:NO];
[self.navigationItem setTitleView:self.searchController.searchBar];
[self setDefinesPresentationContext:YES];
[self.searchController.searchBar setDelegate:self];
[self.searchController.searchBar setShowsCancelButton:YES];
[self.searchController.searchBar setPlaceholder:@"City or Airfield"];
[self.searchController.searchBar sizeToFit];
}
我尝试激活我的搜索控制器,调用[self.searchController.searchBar becomeFirstResponder] 并直接调用searchBarSearchButtonClicked,但没有任何效果。
【问题讨论】:
-
is searchController 代码在 viewDidLoad 中
-
是的(我已经编辑了帖子)
-
只是为了确保在您点击搜索栏时使用当前代码是否一切正常......对吗?你把“[self.searchController.searchBar becomeFirstResponder]”放在 viewDidLoad 的什么地方?
-
是的,一切正常。我首先在 viewDidLoad 中尝试,然后在 viewDidAppear 中尝试了相同的结果,没有。
-
所以你已经在初始化搜索栏后放置了'[self.searchController.searchBar becomeFirstResponder]',对吗?
标签: ios objective-c uisearchbar uisearchcontroller