动态更改UISearchBar的键盘类型 [英] Dynamically Changing Keyboard Type for a UISearchBar
问题描述
我有一个使用 UISearchBar
和 UISearchDisplayController
的iPhone应用程式。搜索栏有三个范围按钮。我想在选择特定范围按钮时将键盘设置为数字键盘,但当选择任何其他范围按钮时,键盘将成为默认键盘。
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.
UISearchBarDelegate
selectedScopeButtonIndexDidChange:selectedScope
方法如下:
- (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.
有
推荐答案
虽然这个方法可以让键盘在屏幕上自行改变,或者以编程方式隐藏它,然后重新显示它。是对我有用的:
Even though this is kind of a hack, this worked for me:
- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope {
switch (selectedScope) {
case 0:
searchBar.keyboardType = UIKeyboardTypeNumberPad;
break;
default:
searchBar.keyboardType = UIKeyboardTypeDefault;
break;
// Hack: force ui to reflect changed keyboard type
[searchBar resignFirstResponder];
[searchBar becomeFirstResponder];
}
这篇关于动态更改UISearchBar的键盘类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!