Search code examples
iosobjective-cuisearchbaruisearchdisplaycontrolleruisearchbardelegate

How to programmatically change scope selected in searchbar?


I want to change the selected scope after user has selected something from a table view. I have tried this:

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

But the scope button i want is still not selected. My search bar doesn't have a subview of class UISegmentedControl.


Solution

  • I assume you mean the scopes of an UISearchBar and you already have your UISearchBar set up so it shows scopes.

    The UISearchBar has a special property that needs to be set:

    self.searchDisplayController.searchBar.selectedScopeButtonIndex = 1;