如何以编程方式更改搜索栏中选择的范围?

时间:2014-10-15 10:08:47

标签: ios objective-c uisearchbar uisearchdisplaycontroller uisearchbardelegate

我想在用户从表格视图中选择某些内容后更改所选范围。 我试过这个:

for (id view in [self.searchDisplayController.searchBar subviews]) {
    if ([view isMemberOfClass:[UISegmentedControl class]])
        {
            UISegmentedControl *scopeBar = view;
            scopeBar.selectedSegmentIndex = 1;
        }
}

但我仍然没有选择我想要的范围按钮。我的搜索栏没有UISegmentedControl类的子视图。

1 个答案:

答案 0 :(得分:2)

我认为您的意思是UISearchBar的范围,并且您已经设置了UISearchBar,因此它显示了范围。

UISearchBar有一个需要设置的特殊属性:

self.searchDisplayController.searchBar.selectedScopeButtonIndex = 1;