2010-08-23 150 views
30

当用户点击“搜索”按钮后,我想关闭我的UISearchDisplayController,因为我正在从网络加载新数据。如何以编程方式关闭控制器?我已经有了正确的方法,但不知道如何去做。如何以编程方式关闭UISearchDisplayController?

我认为下面会工作,但我错了。

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar { 
    [self.searchDisplayController finalize]; 
} 

回答

68
[self.searchDisplayController setActive:NO animated:YES]; 

享受。

+1

非常感谢。2012年的工作很棒,也很有帮助:) – Xtrician 2012-06-30 08:08:08

+0

这对我没有任何作用。我说过用户点击 - (无效)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {[self.searchDisplayController setActive:NO animated:YES];}但是这并没有让它消失。这个方法被调用但没有结果。 – coolcool1994 2013-07-03 20:27:44

+0

完美。我在viewWillDisappear中使用它。正是我需要的。 – 2013-12-19 23:01:07

3

你需要确保你设置活动为false主线:

dispatch_async(dispatch_get_main_queue(), ^{ 
    [self.searchDisplayController setActive:NO animated:YES]; 
}); 
2
func setActive(_ visible: Bool, 
    animated animated;: Bool) 

如果使用快捷。

+0

其实,对于Swift来说,这个属性是'active' – gigo 2016-01-14 11:37:22