views:

1042

answers:

1

I have an iPhone app that uses a UISearchBar and UISearchDisplayController. The search bar has three scope buttons. I would like the keyboard to be a numeric keypad when a particular scope button is selected, but be a default keyboard when any other scope button is selected.

I have implemented the UISearchBarDelegate selectedScopeButtonIndexDidChange:selectedScope method as follows:

- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope
{
    switch (selectedScope) {
        case DeviceIDScopeIndex:
            searchBar.keyboardType = UIKeyboardTypeNumberPad;
            break;
        default:
            searchBar.keyboardType = UIKeyboardTypeDefault;
            break;
    }
}

I know that the method gets called, and the right case gets triggered when the button in question is selected. But the onscreen keyboard doesn't change unless I tap the Cancel button to hide the keyboard, then tap the search field again to bring up the keyboard again.

Is there a way to get the keyboard to change itself while it's onscreen, or to programmatically hide it and then reshow it?

+3  A: 

Even though this is kind of a hack, this worked for me:

- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope {

    searchBar.keyboardType = <somehow determine needed type>;

    // Hack: force ui to reflect changed keyboard type
    [searchBar resignFirstResponder];
    [searchBar becomeFirstResponder];

}
Heiko Behrens
Thanks. I figured this would be the solution, but hoped for something less "unclean."
Kristopher Johnson