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

标签 ios objective-c uisearchbar uisearchdisplaycontroller uisearchbardelegate

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

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

但我想要的范围按钮仍然没有被选中。我的搜索栏没有类 UISegmentedControl 的 subview 。

最佳答案

我假设您指的是 UISearchBar 的范围,并且您已经设置了 UISearchBar 以显示范围。

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

self.searchDisplayController.searchBar.selectedScopeButtonIndex = 1;

关于ios - 如何以编程方式更改搜索栏中选择的范围?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26379865/

相关文章:

ios - UINavigationbar 会在 UISearchbar 结束搜索后显示,如何隐藏导航栏

ios - View 更改后下推 TableView

ios - iOS 中的 CleverTap 通知设置

iphone - 我的 iPhone 应用程序中的内存泄漏在哪里?

ios - 如何在整个 View 中显示 iad 或 admob

objective-c - 如何在不使用 "setEditing "方法删除控件的情况下启用 UITableViewCell 重新排序?

ios - 是否可以使用观察者跟踪 AVAudioPlayer 对象?

iphone - 关于单位换算的简单问题

iphone - 通过UITextField的UISearchBar功能

ios - UISearchBar - 不显示搜索结果